proxygen
folly::StandardLogHandlerFactory Class Reference

#include <StandardLogHandlerFactory.h>

Classes

class  FormatterFactory
 
class  OptionProcessor
 
class  WriterFactory
 

Public Types

using Options = std::unordered_map< std::string, std::string >
 

Static Public Member Functions

static std::shared_ptr< StandardLogHandlercreateHandler (StringPiece type, WriterFactory *writerFactory, const Options &options)
 

Detailed Description

StandardLogHandlerFactory contains helper methods for LogHandlerFactory implementations that create StandardLogHandler objects.

StandardLogHandlerFactory does not derive from LogHandlerFactory itself.

Definition at line 35 of file StandardLogHandlerFactory.h.

Member Typedef Documentation

Definition at line 37 of file StandardLogHandlerFactory.h.

Member Function Documentation

std::shared_ptr< StandardLogHandler > folly::StandardLogHandlerFactory::createHandler ( StringPiece  type,
WriterFactory writerFactory,
const Options options 
)
static

Definition at line 92 of file StandardLogHandlerFactory.cpp.

References folly::StandardLogHandlerFactory::WriterFactory::createWriter(), folly::get_ptr(), handled, folly::join(), folly::StandardLogHandlerFactory::OptionProcessor::processOption(), folly::stringToLogLevel(), and type.

Referenced by folly::StreamHandlerFactory::createHandler(), folly::FileHandlerFactory::createHandler(), and folly::TestHandlerFactory::createHandler().

95  {
96  std::unique_ptr<FormatterFactory> formatterFactory;
97 
98  // Get the log formatter type
99  auto* formatterType = get_ptr(options, "formatter");
100  if (!formatterType || *formatterType == "glog") {
101  formatterFactory = std::make_unique<GlogFormatterFactory>();
102  } else if (!formatterType || *formatterType == "custom") {
103  formatterFactory = std::make_unique<CustomLogFormatterFactory>();
104  } else {
105  throw std::invalid_argument(
106  to<string>("unknown log formatter type \"", *formatterType, "\""));
107  }
108 
109  Optional<LogLevel> syncLevel;
110 
111  // Process the log formatter and log handler options
112  std::vector<string> errors;
113  for (const auto& entry : options) {
114  bool handled = false;
115  try {
116  // Allow both the formatterFactory and writerFactory to consume an
117  // option. In general they probably should have mutually exclusive
118  // option names, but we don't give precedence to one over the other here.
119  handled |= formatterFactory->processOption(entry.first, entry.second);
120  handled |= writerFactory->processOption(entry.first, entry.second);
121  } catch (const std::exception& ex) {
122  errors.push_back(to<string>(
123  "error processing option \"", entry.first, "\": ", ex.what()));
124  continue;
125  }
126 
127  // We explicitly processed the "formatter" option above.
128  handled |= handled || (entry.first == "formatter");
129 
130  // Process the "sync_level" option.
131  if (entry.first == "sync_level") {
132  try {
133  syncLevel = stringToLogLevel(entry.second);
134  } catch (const std::exception& ex) {
135  errors.push_back(to<string>(
136  "unable to parse value for option \"",
137  entry.first,
138  "\": ",
139  ex.what()));
140  }
141  handled = true;
142  }
143 
144  // Complain about unknown options.
145  if (!handled) {
146  errors.push_back(to<string>("unknown option \"", entry.first, "\""));
147  }
148  }
149 
150  if (!errors.empty()) {
151  throw std::invalid_argument(join(", ", errors));
152  }
153 
154  // Construct the formatter and writer
155  auto writer = writerFactory->createWriter();
156  auto formatter = formatterFactory->createFormatter(writer);
157 
158  if (syncLevel) {
159  return std::make_shared<StandardLogHandler>(
160  LogHandlerConfig{type, options}, formatter, writer, *syncLevel);
161  } else {
162  return std::make_shared<StandardLogHandler>(
163  LogHandlerConfig{type, options}, formatter, writer);
164  }
165 }
const Map::mapped_type * get_ptr(const Map &map, const Key &key)
Definition: MapUtil.h:169
volatile bool handled
PskType type
virtual bool processOption(StringPiece name, StringPiece value)=0
LogLevel stringToLogLevel(StringPiece name)
Definition: LogLevel.cpp:42
virtual std::shared_ptr< LogFormatter > createFormatter(const std::shared_ptr< LogWriter > &logWriter)=0
void join(const Delim &delimiter, Iterator begin, Iterator end, String &output)
Definition: String-inl.h:498

The documentation for this class was generated from the following files: