peerplays-fc/src/log.cpp

66 lines
1.6 KiB
C++
Raw Normal View History

2012-09-08 02:50:37 +00:00
#include <fc/log.hpp>
2012-09-09 23:44:49 +00:00
#include <fc/unique_lock.hpp>
#include <boost/thread/mutex.hpp>
2012-09-08 02:50:37 +00:00
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
2012-12-03 19:51:31 +00:00
#ifndef WIN32
2012-09-08 02:50:37 +00:00
#include <unistd.h>
2012-12-03 19:51:31 +00:00
#endif
2012-09-08 02:50:37 +00:00
2012-12-12 18:41:33 +00:00
#include <fc/filesystem.hpp>
2012-09-11 03:13:31 +00:00
#include <iostream>
2012-09-08 02:50:37 +00:00
namespace fc {
2012-09-09 15:34:26 +00:00
const char* thread_name();
2012-09-09 23:44:49 +00:00
void* thread_ptr();
2012-09-09 15:34:26 +00:00
2012-12-12 18:41:33 +00:00
/*
2012-09-08 02:50:37 +00:00
const char* short_name( const char* file_name ) {
const char* end = file_name + strlen(file_name);
--end;
while( end >= file_name ) {
if( *end == '/' || *end == '\\' ) {
return end + 1;
}
--end;
}
return file_name;
}
2012-12-12 18:41:33 +00:00
*/
2012-09-08 02:50:37 +00:00
#ifdef WIN32
#define isatty _isatty
#define fileno _fileno
#endif // WIN32
2012-09-09 23:44:49 +00:00
void log( const char* color, const char* file_name, size_t line_num,
const char* method_name, const char* format, ... ) {
fc::unique_lock<boost::mutex> lock(log_mutex());
2012-12-03 19:51:31 +00:00
#ifndef WIN32
2012-09-08 02:50:37 +00:00
if(isatty(fileno(stderr)))
2012-09-11 03:13:31 +00:00
std::cerr<<"\r"<<color;
2012-12-03 19:51:31 +00:00
#endif
2012-09-11 03:13:31 +00:00
2012-12-12 18:41:33 +00:00
fprintf( stderr, "%-15s %-15s %-5zd %-15s ", thread_name(), fc::path(file_name).filename().generic_string().c_str(), line_num, method_name );
//std::cerr<<thread_ptr()<< thread_name()<< short_name(file_name)<< line_num<< method_name ;
2012-09-08 02:50:37 +00:00
va_list args;
va_start(args,format);
vfprintf( stderr, format, args );
va_end(args);
2012-12-03 19:51:31 +00:00
#ifndef WIN32
2012-09-08 02:50:37 +00:00
if (isatty(fileno(stderr)))
fprintf( stderr, "%s", CONSOLE_DEFAULT );
2012-12-03 19:51:31 +00:00
#endif
2012-09-08 02:50:37 +00:00
fprintf( stderr, "\n" );
2012-09-30 21:02:44 +00:00
fflush( stderr );
2012-09-08 02:50:37 +00:00
}
/** used to add extra fields to be printed (thread,fiber,time,etc) */
void add_log_field( void (*f)( ) ) {
}
void remove_log_field( void (*f)( ) ) {
}
}