30 class GlogFormatterFactory
31 :
public StandardLogHandlerFactory::FormatterFactory {
36 std::shared_ptr<LogFormatter> createFormatter(
37 const std::shared_ptr<LogWriter>& )
override {
38 return std::make_shared<GlogStyleFormatter>();
42 class CustomLogFormatterFactory
43 :
public StandardLogHandlerFactory::FormatterFactory {
45 enum Colored { ALWAYS, AUTO, NEVER };
48 if (name ==
"log_format") {
51 }
else if (name ==
"colored") {
52 if (value ==
"always") {
54 }
else if (value ==
"auto") {
56 }
else if (value ==
"never") {
59 throw std::invalid_argument(to<string>(
60 "unknown colored type \"",
62 "\". Needs to be always/never/auto"));
69 std::shared_ptr<LogFormatter> createFormatter(
70 const std::shared_ptr<LogWriter>& logWriter)
override {
77 colored = logWriter->ttyOutput();
83 return std::make_shared<CustomLogFormatter>(
format_, colored);
96 std::unique_ptr<FormatterFactory> formatterFactory;
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>();
105 throw std::invalid_argument(
106 to<string>(
"unknown log formatter type \"", *formatterType,
"\""));
112 std::vector<string> errors;
113 for (
const auto& entry : options) {
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()));
128 handled |= handled || (entry.first ==
"formatter");
131 if (entry.first ==
"sync_level") {
134 }
catch (
const std::exception& ex) {
135 errors.push_back(to<string>(
136 "unable to parse value for option \"",
146 errors.push_back(to<string>(
"unknown option \"", entry.first,
"\""));
150 if (!errors.empty()) {
151 throw std::invalid_argument(
join(
", ", errors));
156 auto formatter = formatterFactory->createFormatter(writer);
159 return std::make_shared<StandardLogHandler>(
162 return std::make_shared<StandardLogHandler>(
const Map::mapped_type * get_ptr(const Map &map, const Key &key)
static std::shared_ptr< StandardLogHandler > createHandler(StringPiece type, WriterFactory *writerFactory, const Options &options)
virtual std::shared_ptr< LogWriter > createWriter()=0
virtual bool processOption(StringPiece name, StringPiece value)=0
—— Concurrent Priority Queue Implementation ——
LogLevel stringToLogLevel(StringPiece name)
std::unordered_map< std::string, std::string > Options
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
void join(const Delim &delimiter, Iterator begin, Iterator end, String &output)
Range< const char * > StringPiece