@@ -22,7 +22,8 @@ namespace mpu {
2222// --------------------
2323
2424// Variables for logToString
25- const std::string LogLvlToString[] = {" NOLOG" , " FATAL_ERROR" , " ERROR" , " WARNING" , " INFO" , " DEBUG" , " DEBUG1" , " DEBUG2" , " ALL" };
25+ const std::string LogLvlToString[] = {" NOLOG" , " FATAL_ERROR" , " ERROR" , " WARNING" , " INFO" , " DEBUG" , " DEBUG1" , " DEBUG2" ,
26+ " ALL" };
2627const std::string LogLvlStringInvalid = " INVALID" ;
2728const std::string LogPolicyToString[] = {" NONE" , " CONSOLE" , " FILE" , " SYSLOG" , " CUSTOM" };
2829
@@ -35,7 +36,7 @@ Log::Log(LogPolicy policy, const std::string &sFile, const std::string &sErrorFi
3536 sTimeFormat = " %c" ;
3637 logLvl = lvl;
3738 currentLvl = LogLvl::invalid;
38- logPolicy = LogPolicy::none ;
39+ logPolicy = LogPolicy::none;
3940 outStream = nullptr ;
4041 errorStream = nullptr ;
4142
@@ -52,33 +53,35 @@ Log::Log(LogPolicy policy, std::ostream *out, std::ostream *err, LogLvl lvl)
5253 sTimeFormat = " %c" ;
5354 logLvl = lvl;
5455 currentLvl = LogLvl::invalid;
55- logPolicy = LogPolicy::none ;
56+ logPolicy = LogPolicy::none;
5657 outStream = nullptr ;
5758 errorStream = nullptr ;
5859
59- open (policy,out,err);
60+ open (policy, out, err);
6061
6162 // first log created is going to be global
62- if (noGlobal ())
63+ if (noGlobal ())
6364 makeGlobal ();
6465}
6566
6667#ifdef __linux__
67- Log::Log (LogPolicy policy, const std::string &sIdent , int iFacility, LogLvl lvl)
68- {
69- sTimeFormat = " %c" ;
70- logLvl = lvl;
71- currentLvl = LogLvl::invalid;
72- logPolicy = LogPolicy::none ;
73- outStream = nullptr ;
74- errorStream = nullptr ;
7568
76- open (policy, sIdent , iFacility);
69+ Log::Log (LogPolicy policy, const std::string &sIdent , int iFacility, LogLvl lvl)
70+ {
71+ sTimeFormat = " %c" ;
72+ logLvl = lvl;
73+ currentLvl = LogLvl::invalid;
74+ logPolicy = LogPolicy::none;
75+ outStream = nullptr ;
76+ errorStream = nullptr ;
77+
78+ open (policy, sIdent , iFacility);
79+
80+ // first log created is going to be global
81+ if (noGlobal ())
82+ makeGlobal ();
83+ }
7784
78- // first log created is going to be global
79- if (noGlobal ())
80- makeGlobal ();
81- }
8285#endif
8386
8487Log::~Log ()
@@ -89,31 +92,31 @@ Log::~Log()
8992void Log::open (LogPolicy policy, const std::string &sFile , const std::string &sErrorFile )
9093{
9194 // close in case it is already opened
92- if (logPolicy != LogPolicy::none)
95+ if (logPolicy != LogPolicy::none)
9396 close ();
9497
95- switch (policy)
98+ switch (policy)
9699 {
97- case console:
98- outStream = &std::cout;
99- errorStream = &std::cerr;
100- break ;
101-
102- case file:
103- outStream = new std::ofstream (sFile , std::ofstream::out | std::ofstream::app);
104- if (sErrorFile .empty ())
105- errorStream = outStream;
106- else
107- errorStream = new std::ofstream (sErrorFile , std::ofstream::out | std::ofstream::app);
108-
109- if (!outStream || !dynamic_cast <std::ofstream*>(outStream)->is_open ())
110- throw std::runtime_error (" Log: Could not open output file stream!" );
111- if (!errorStream || !dynamic_cast <std::ofstream*>(errorStream)->is_open ())
112- throw std::runtime_error (" Log: Could not open output file stream!" );
113- break ;
114-
115- default :
116- throw std::invalid_argument (" Log: You called the wrong open function/constructor for your policy!" );
100+ case console:
101+ outStream = &std::cout;
102+ errorStream = &std::cerr;
103+ break ;
104+
105+ case file:
106+ outStream = new std::ofstream (sFile , std::ofstream::out | std::ofstream::app);
107+ if (sErrorFile .empty ())
108+ errorStream = outStream;
109+ else
110+ errorStream = new std::ofstream (sErrorFile , std::ofstream::out | std::ofstream::app);
111+
112+ if (!outStream || !dynamic_cast <std::ofstream *>(outStream)->is_open ())
113+ throw std::runtime_error (" Log: Could not open output file stream!" );
114+ if (!errorStream || !dynamic_cast <std::ofstream *>(errorStream)->is_open ())
115+ throw std::runtime_error (" Log: Could not open output file stream!" );
116+ break ;
117+
118+ default :
119+ throw std::invalid_argument (" Log: You called the wrong open function/constructor for your policy!" );
117120 }
118121
119122 logPolicy = policy;
@@ -122,15 +125,15 @@ void Log::open(LogPolicy policy, const std::string &sFile, const std::string &sE
122125void Log::open (LogPolicy policy, std::ostream *out, std::ostream *err)
123126{
124127 // close in case it is already opened
125- if (logPolicy != LogPolicy::none)
128+ if (logPolicy != LogPolicy::none)
126129 close ();
127130
128- if (policy != LogPolicy::custom)
131+ if (policy != LogPolicy::custom)
129132 throw std::invalid_argument (" Log: You called the wrong open function/constructor for your policy!" );
130133
131134 outStream = out;
132135
133- if (err)
136+ if (err)
134137 errorStream = err;
135138 else
136139 errorStream = out;
@@ -139,65 +142,67 @@ void Log::open(LogPolicy policy, std::ostream *out, std::ostream *err)
139142}
140143
141144#ifdef __linux__
145+
142146void Log::open (LogPolicy policy, const std::string &sIdent , int iFacility)
143147{
144148 // close in case it is already opened
145- if (logPolicy != LogPolicy::none)
149+ if (logPolicy != LogPolicy::none)
146150 close ();
147151
148- if (policy != LogPolicy::syslog)
152+ if (policy != LogPolicy::syslog)
149153 throw std::invalid_argument (" Log: You called the wrong open function/constructor for your policy!" );
150154
151155 // use the an ostream with the syslog streambuffer
152- outStream = new std::ostream ( new SyslogStreambuf (sIdent , iFacility, this ));
156+ outStream = new std::ostream (new SyslogStreambuf (sIdent , iFacility, this ));
153157 errorStream = outStream;
154158
155159 // turn of timestamp, since syslog already provides a timestamp
156160 setTimeFormat (" " );
157161 logPolicy = policy;
158162}
163+
159164#endif
160165
161166void Log::close ()
162167{
163168 flush ();
164- switch (logPolicy)
169+ switch (logPolicy)
165170 {
166- case file:
167- dynamic_cast <std::ofstream*>(outStream)->close ();
168- dynamic_cast <std::ofstream*>(errorStream)->close ();
169- if (outStream == errorStream)
170- {
171- MPU_SAVE_DELETE (outStream);
172- errorStream = nullptr ;
173- }
174- else
175- {
176- MPU_SAVE_DELETE (outStream);
177- MPU_SAVE_DELETE (errorStream);
178- }
179- break ;
180- case syslog:
181- // reset the timestamp string
182- setTimeFormat (" %c" );
183- if (outStream == errorStream)
184- {
185- MPU_SAVE_DELETE (outStream);
186- errorStream = nullptr ;
187- }
188- else
189- {
190- MPU_SAVE_DELETE (outStream);
191- MPU_SAVE_DELETE (errorStream);
192- }
193- break ;
194- case console:
195- case custom:
196- outStream = nullptr ;
171+ case file:
172+ dynamic_cast <std::ofstream *>(outStream)->close ();
173+ dynamic_cast <std::ofstream *>(errorStream)->close ();
174+ if (outStream == errorStream)
175+ {
176+ MPU_SAVE_DELETE (outStream);
197177 errorStream = nullptr ;
198- break ;
199- default :
200- break ;
178+ }
179+ else
180+ {
181+ MPU_SAVE_DELETE (outStream);
182+ MPU_SAVE_DELETE (errorStream);
183+ }
184+ break ;
185+ case syslog:
186+ // reset the timestamp string
187+ setTimeFormat (" %c" );
188+ if (outStream == errorStream)
189+ {
190+ MPU_SAVE_DELETE (outStream);
191+ errorStream = nullptr ;
192+ }
193+ else
194+ {
195+ MPU_SAVE_DELETE (outStream);
196+ MPU_SAVE_DELETE (errorStream);
197+ }
198+ break ;
199+ case console:
200+ case custom:
201+ outStream = nullptr ;
202+ errorStream = nullptr ;
203+ break ;
204+ default :
205+ break ;
201206 }
202207 logPolicy = LogPolicy::none;
203208}
0 commit comments