@@ -39,40 +39,49 @@ std::string Now() {
39
39
return std::string (time_str);
40
40
}
41
41
42
- class NoOpLogger : public Log {
42
+ class NoOpLogger : public LogImpl {
43
43
public:
44
- NoOpLogger (Options options) : Log(options) {}
44
+ NoOpLogger () = default ;
45
45
~NoOpLogger () override = default ;
46
46
47
- protected:
48
47
void WriteLine (const std::string &line) override {}
49
48
};
50
49
51
- class SysLogLogger : public Log {
50
+ class SysLogLogger : public LogImpl {
52
51
public:
53
- SysLogLogger (Options options) : Log(options) {
54
- openlog (" sasl-xoauth2" , 0 , 0 );
55
- }
52
+ SysLogLogger () = default ;
53
+ ~SysLogLogger () override = default ;
56
54
57
- ~SysLogLogger () override { closelog (); }
58
-
59
- protected:
60
55
void WriteLine (const std::string &line) override {
56
+ openlog (" sasl-xoauth2" , 0 , 0 );
61
57
syslog (LOG_WARNING, " %s\n " , line.c_str ());
58
+ closelog ();
62
59
}
63
60
};
64
61
65
- class StdErrLogger : public Log {
62
+ class StdErrLogger : public LogImpl {
66
63
public:
67
- StdErrLogger (Options options) : Log(options) {}
64
+ StdErrLogger () = default ;
68
65
~StdErrLogger () override = default ;
69
66
70
- protected:
71
67
void WriteLine (const std::string &line) override {
72
68
fprintf (stderr, " %s\n " , line.c_str ());
73
69
}
74
70
};
75
71
72
+ std::unique_ptr<LogImpl> CreateLogImpl (Log::Target target) {
73
+ switch (target) {
74
+ case Log::TARGET_NONE:
75
+ return std::make_unique<NoOpLogger>();
76
+ case Log::TARGET_SYSLOG:
77
+ return std::make_unique<SysLogLogger>();
78
+ case Log::TARGET_STDERR:
79
+ return std::make_unique<StdErrLogger>();
80
+ default :
81
+ exit (1 );
82
+ };
83
+ }
84
+
76
85
} // namespace
77
86
78
87
void EnableLoggingForTesting () {
@@ -83,16 +92,7 @@ void EnableLoggingForTesting() {
83
92
std::unique_ptr<Log> Log::Create (Options options, Target target) {
84
93
options = static_cast <Options>(options | s_default_options);
85
94
if (target == TARGET_DEFAULT) target = s_default_target;
86
- switch (target) {
87
- case TARGET_NONE:
88
- return std::make_unique<NoOpLogger>(options);
89
- case TARGET_SYSLOG:
90
- return std::make_unique<SysLogLogger>(options);
91
- case TARGET_STDERR:
92
- return std::make_unique<StdErrLogger>(options);
93
- default :
94
- return {};
95
- };
95
+ return std::unique_ptr<Log>(new Log (CreateLogImpl (target), options));
96
96
}
97
97
98
98
Log::~Log () {
@@ -108,24 +108,23 @@ void Log::Write(const char *fmt, ...) {
108
108
109
109
const std::string line = buf;
110
110
if (options_ & OPTIONS_IMMEDIATE) {
111
- WriteLine (line);
111
+ impl_-> WriteLine (line);
112
112
} else {
113
113
lines_.push_back (Now () + " : " + line);
114
114
}
115
115
}
116
116
117
117
void Log::Flush () {
118
118
if (lines_.empty ()) return ;
119
-
120
119
if (options_ & OPTIONS_FULL_TRACE_ON_FAILURE) {
121
- WriteLine (" auth failed:" );
122
- for (const auto &line : lines_) WriteLine (" " + line);
120
+ impl_-> WriteLine (" auth failed:" );
121
+ for (const auto &line : lines_) impl_-> WriteLine (" " + line);
123
122
} else {
124
123
if (summary_.empty ()) summary_ = lines_.back ();
125
- WriteLine (" auth failed: " + summary_);
124
+ impl_-> WriteLine (" auth failed: " + summary_);
126
125
if (lines_.size () > 1 ) {
127
- WriteLine (" set log_full_trace_on_failure to see full " +
128
- std::to_string (lines_.size ()) + " line(s) of tracing." );
126
+ impl_-> WriteLine (" set log_full_trace_on_failure to see full " +
127
+ std::to_string (lines_.size ()) + " line(s) of tracing." );
129
128
}
130
129
}
131
130
}
0 commit comments