33 bool verbose_log ,
bool debug ,
bool level ,
bool timestamp ,
bool strip ,
34 bool use_syslog ,
const std::string & stderr_replacement ,
SyslogFacility syslog_facility ) :
37 m_summary_log(summary_log) ,
38 m_verbose_log(verbose_log) ,
42 m_syslog(use_syslog) ,
43 m_std_err(err(stderr_replacement)) ,
44 m_facility(syslog_facility) ,
46 m_timestamp(timestamp) ,
56 const std::string & stderr_replacement ) :
57 m_enabled(enabled_and_summary) ,
58 m_summary_log(enabled_and_summary) ,
59 m_verbose_log(verbose_and_debug) ,
60 m_debug(verbose_and_debug) ,
64 m_std_err(err(stderr_replacement)) ,
76 std::ostream & G::LogOutput::err(
const std::string & path_in )
78 if( !path_in.empty() )
80 std::string path = path_in ;
82 if( pos != std::string::npos )
83 path.replace( pos , 2U , dateString() ) ;
85 static std::ofstream file( path.c_str() , std::ios_base::out | std::ios_base::app ) ;
115 bool was_enabled = m_enabled ;
116 m_enabled = enabled ;
122 if( instance() != NULL )
123 instance()->doOutput( severity , file , line , text ) ;
126 void G::LogOutput::doOutput(
Log::Severity severity ,
const char * ,
int ,
const std::string & text )
129 bool do_output = m_enabled ;
131 do_output = m_enabled && m_debug ;
133 do_output = m_enabled && m_summary_log ;
135 do_output = m_enabled && m_verbose_log ;
142 buffer.reserve( (text.length()>limit?limit:text.length()) + 40U ) ;
146 if( m_prefix.length() )
148 buffer.append( m_prefix ) ;
149 buffer.append(
": " ) ;
152 buffer.append( timestampString() ) ;
154 buffer.append( levelString(severity) ) ;
157 text_pos = text.find(
' ') ;
158 if( text_pos == std::string::npos || (text_pos+1U) == text.length() )
165 size_type text_len = text.length() - text_pos ;
166 bool limited = text_len > limit ;
167 text_len = text_len > limit ? limit : text_len ;
168 buffer.append( text , text_pos , text_len ) ;
170 buffer.append(
" ..." ) ;
173 while( buffer.find(
'\033') != std::string::npos )
174 buffer[buffer.find(
'\033')] =
'.' ;
177 rawOutput( m_std_err , severity , buffer ) ;
186 std::string G::LogOutput::timestampString()
189 std::time_t now = std::time(NULL) ;
190 if( m_time == 0 || m_time != now )
193 static struct std::tm zero_broken_down_time ;
194 struct std::tm broken_down_time = zero_broken_down_time ;
195 getLocalTime( m_time , &broken_down_time ) ;
196 m_time_buffer[0] =
'\0' ;
197 std::strftime( m_time_buffer,
sizeof(m_time_buffer)-1U,
"%Y" "%m" "%d." "%H" "%M" "%S: ", &broken_down_time ) ;
198 m_time_buffer[
sizeof(m_time_buffer)-1U] =
'\0' ;
200 return std::string(m_time_buffer) ;
203 std::string G::LogOutput::dateString()
205 static struct std::tm zero_broken_down_time ;
206 struct std::tm broken_down_time = zero_broken_down_time ;
207 getLocalTime( std::time(NULL) , &broken_down_time ) ;
208 char buffer[10] = { 0 } ;
209 std::strftime( buffer ,
sizeof(buffer)-1U ,
"%Y" "%m" "%d" , &broken_down_time ) ;
210 buffer[
sizeof(buffer)-1U] =
'\0' ;
211 return std::string( buffer ) ;
214 std::string G::LogOutput::fileAndLine(
const char * file ,
int line )
218 std::string basename( file ) ;
220 if( slash_pos != std::string::npos && (slash_pos+1U) < basename.length() )
221 basename.erase( 0U , slash_pos+1U ) ;
222 return basename +
"(" + itoa(line) +
"): " ;
226 return std::string() ;
235 instance()->doAssertion( file , line , test_string ) ;
237 std::cerr <<
"assertion error: " << file <<
"(" << line <<
"): " << test_string << std::endl ;
242 void G::LogOutput::doAssertion(
const char * file ,
int line ,
const std::string & test_string )
249 std::string() +
"Assertion error: " + fileAndLine(file,line) + test_string ) ;
252 void G::LogOutput::halt()
265 return std::string() ;
268 std::string G::LogOutput::itoa(
int n_ )
271 if( n_ < 0 )
return std::string(1U,
'0') ;
272 char buffer[20] = {
'0' ,
'\0' } ;
273 unsigned int buffer_size =
sizeof(buffer) ;
274 unsigned int n =
static_cast<unsigned int>(n_) ;
276 bool zero = n == 0U ;
277 char * p = buffer + buffer_size - 1U ;
278 for( *p-- =
'\0' ; n > 0U ; --p , n /= 10U )
279 *p = static_cast<char>(
'0' + (n % 10U) ) ;
280 return zero ? buffer : (p+1U) ;
bool enable(bool enabled=true)
Enables or disables output. Returns the previous setting.
static LogOutput * instance()
Returns a pointer to the controlling LogOutput object.
LogOutput(const std::string &prefix, bool output, bool with_logging, bool with_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.
std::string::size_type size_type
A std::size_t type.
static void assertion(const char *file, int line, bool test, const std::string &)
Makes an assertion check (regardless of any LogOutput object).
virtual void onAssert()
Called during an assertion failure.
virtual ~LogOutput()
Destructor.
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.
Controls and implements low-level logging output, as used by the Log interface.