35 bool verbose_log ,
bool debug ,
bool with_level ,
bool with_timestamp ,
bool strip ,
36 bool use_syslog ,
const std::string & stderr_replacement , SyslogFacility syslog_facility ) :
39 m_summary_log(summary_log) ,
40 m_verbose_log(verbose_log) ,
45 m_syslog(use_syslog) ,
46 m_std_err(err(stderr_replacement)) ,
47 m_facility(syslog_facility) ,
49 m_timestamp(with_timestamp) ,
53 if( pthis() ==
nullptr )
60 const std::string & stderr_replacement ) :
61 m_enabled(enabled_and_summary) ,
62 m_summary_log(enabled_and_summary) ,
63 m_verbose_log(verbose_and_debug) ,
65 m_debug(verbose_and_debug) ,
69 m_std_err(err(stderr_replacement)) ,
76 if( pthis() ==
nullptr )
82 std::ostream & G::LogOutput::err(
const std::string & path_in )
84 if( !path_in.empty() )
86 std::string path = path_in ;
87 std::string::size_type pos = path.find(
"%d") ;
88 if( pos != std::string::npos )
89 path.replace( pos , 2U , dateString() ) ;
91 static std::ofstream file( path.c_str() , std::ios_base::out | std::ios_base::app ) ;
103 if( pthis() ==
this )
121 bool was_enabled = m_enabled ;
122 m_enabled = enabled ;
133 m_verbose_log = verbose_log ;
138 if( instance() !=
nullptr )
139 instance()->doOutput( severity , file , line , text ) ;
144 bool do_output = m_enabled ;
145 if( severity == Log::s_Debug )
146 do_output = m_enabled && m_debug ;
147 else if( severity == Log::s_LogSummary )
148 do_output = m_enabled && m_summary_log ;
149 else if( severity == Log::s_LogVerbose )
150 do_output = m_enabled && m_verbose_log ;
158 instance()->m_groups.clear() ;
159 const char sep =
',' ;
161 const size_t npos = std::string::npos ;
162 for(
size_t p2 = list.find(sep,p1) ; p1 < list.size() ; p1 = p2==npos?npos:(p2+1U) , p2 = list.find(sep,p1) )
165 instance()->m_groups.insert( list.substr( p1 , p2==npos ? p2 : (p2-p1) ) ) ;
172 if( m_groups.empty() )
173 return at( severity ) ;
175 return at(severity) && ( m_groups.find(
"log-all") != m_groups.end() || m_groups.find(
"log-"+group) != m_groups.end() ) ;
178 void G::LogOutput::doOutput( Log::Severity severity ,
const char * ,
int ,
const std::string & text )
180 bool do_output =
at( severity ) ;
184 const size_type limit =
static_cast<size_type
>(limits::log) ;
185 m_buffer.reserve( (text.length()>limit?limit:text.length()) + 40U ) ;
189 std::string::size_type text_pos = 0U ;
190 if( m_prefix.length() )
192 m_buffer.append( m_prefix ) ;
193 m_buffer.append(
": " ) ;
196 appendTimestampStringTo( m_buffer ) ;
198 m_buffer.append( levelString(severity) ) ;
203 text_pos = text.find(
' ') ;
204 if( text_pos == std::string::npos || (text_pos+1U) == text.length() )
211 size_type text_len = text.length() - text_pos ;
212 bool limited = text_len > limit ;
213 text_len = text_len > limit ? limit : text_len ;
214 m_buffer.append( text , text_pos , text_len ) ;
216 m_buffer.append(
" ..." ) ;
219 while( m_buffer.find(
'\033') != std::string::npos )
220 m_buffer[m_buffer.find(
'\033')] =
'.' ;
223 rawOutput( m_std_err , severity , m_buffer ) ;
232 void G::LogOutput::appendTimestampStringTo( std::string & result )
236 if( m_time == 0 || m_time != now.s )
239 static struct std::tm zero_broken_down_time ;
240 struct std::tm broken_down_time = zero_broken_down_time ;
241 getLocalTime( m_time , &broken_down_time ) ;
242 m_time_buffer.reserve( 30U ) ;
243 m_time_buffer.assign( 17U ,
'\0' ) ;
244 std::strftime( &m_time_buffer[0] , m_time_buffer.size() ,
"%Y" "%m" "%d." "%H" "%M" "%S.", &broken_down_time ) ;
245 m_time_buffer.pop_back() ;
248 result.append( &m_time_buffer[0] , m_time_buffer.size() ) ;
249 result.append( 1U ,
'0' + ( ( now.us / 100000 ) % 10 ) ) ;
250 result.append( 1U ,
'0' + ( ( now.us / 10000 ) % 10 ) ) ;
251 result.append( 1U ,
'0' + ( ( now.us / 1000 ) % 10 ) ) ;
252 result.append(
": " ) ;
255 std::string G::LogOutput::dateString()
258 static struct std::tm zero_broken_down_time ;
259 struct std::tm broken_down_time = zero_broken_down_time ;
260 getLocalTime( std::time(
nullptr) , &broken_down_time ) ;
261 char buffer[10] = { 0 } ;
262 std::strftime( buffer ,
sizeof(buffer)-1U ,
"%Y" "%m" "%d" , &broken_down_time ) ;
263 buffer[
sizeof(buffer)-1U] =
'\0' ;
264 return std::string( buffer ) ;
267 std::string G::LogOutput::fileAndLine(
const char * file ,
int line )
269 if( file !=
nullptr )
271 std::string basename( file ) ;
272 std::string::size_type slash_pos = basename.find_last_of(
"/\\" ) ;
273 if( slash_pos != std::string::npos && (slash_pos+1U) < basename.length() )
274 basename.erase( 0U , slash_pos+1U ) ;
275 return basename +
"(" + itoa(line) +
"): " ;
279 return std::string() ;
288 instance()->doAssertion( file , line , test_string ) ;
290 std::cerr <<
"assertion error: " << file <<
"(" << line <<
"): " << test_string << std::endl ;
295 void G::LogOutput::doAssertion(
const char * file ,
int line ,
const std::string & test_string )
301 rawOutput( m_std_err , Log::s_Assertion ,
302 std::string() +
"Assertion error: " + fileAndLine(file,line) + test_string ) ;
305 void G::LogOutput::halt()
310 const char * G::LogOutput::levelString( Log::Severity s )
312 if( s == Log::s_Debug )
return "debug: " ;
313 else if( s == Log::s_LogSummary )
return "info: " ;
314 else if( s == Log::s_LogVerbose )
return "info: " ;
315 else if( s == Log::s_Warning )
return "warning: " ;
316 else if( s == Log::s_Error )
return "error: " ;
317 else if( s == Log::s_Assertion )
return "fatal: " ;
321 std::string G::LogOutput::itoa(
int n_ )
324 if( n_ < 0 )
return std::string(1U,
'0') ;
325 char buffer[20] = {
'0' ,
'\0' } ;
326 unsigned int buffer_size =
sizeof(buffer) ;
327 unsigned int n =
static_cast<unsigned int>(n_) ;
329 bool zero = n == 0U ;
330 char * p = buffer + buffer_size - 1U ;
331 for( *p-- =
'\0' ; n > 0U ; --p , n /= 10U )
332 *p = static_cast<char>(
'0' + (n % 10U) ) ;
333 return zero ? buffer : (p+1U) ;
void quiet(bool quiet_stderr=true)
Enables or disables quiet-stderr mode, intended for daemons that log to syslog and only want startup ...
LogOutput(const std::string &prefix, bool output, bool summary_logging, bool verbose_logging, bool with_debug, bool with_level, bool with_timestamp, bool strip_context, bool use_syslog, const std::string &stderr_replacement=std::string(), SyslogFacility syslog_facility=User)
Constructor.
A subsecond-resolution timestamp based on a time_t.
static std::string get(const std::string &name, const std::string &default_)
Returns the environment variable value or the given default.
bool syslog() const
Returns true if syslog output is enabled.
bool enable(bool enabled=true)
Enables or disables output. Returns the previous setting.
static bool at(Severity)
Returns true if G::LogOutput::output() would log at the given level.
static LogOutput * instance()
Returns a pointer to the controlling LogOutput object.
virtual void onAssert()
Called during an assertion failure.
static void groups(const std::string &)
Sets the list of active logging groups.
static EpochTime now()
Returns the current epoch time.
virtual ~LogOutput()
Destructor.
static void assertion(const char *file, int line, bool test, const char *)
Makes an assertion check (regardless of any LogOutput object).
static void output(G::Log::Severity, const char *file, int line, const std::string &)
Generates output if there is an existing LogOutput object which is enabled.
bool at(G::Log::Severity) const
Returns true if logging should occur for the given severity level.
Controls and implements low-level logging output, as used by the Log interface.
void verbose(bool verbose_log=true)
Enables or disables verbose logging.