00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifdef HAVE_CONFIG_H
00017 #include "torc/generic/config.h"
00018 #endif //HAVE_CONFIG_H
00019
00020 #ifdef LOGGING
00021 #include <cstdio>
00022 #include <cstdarg>
00023 #endif
00024
00025 #include "torc/generic/util/Log.hpp"
00026
00027 namespace {
00028
00029 #ifdef LOGGING
00030
00031 class Logger {
00032 public:
00033 void
00034 write( const char *fmt, va_list ap );
00035
00036 void
00037 openLogFile( const std::string &inFileName );
00038
00039 Logger();
00040 ~Logger();
00041
00042 static Logger *
00043 instance();
00044
00045 private:
00046 FILE *mStream;
00047 };
00048
00049
00050 void
00051 Logger::write( const char *fmt, va_list ap )
00052 {
00053 vfprintf( mStream, fmt, ap );
00054 return;
00055 }
00056
00057 void
00058 Logger::openLogFile( const std::string &inFileName ) {
00059 if( !inFileName.empty() )
00060 {
00061 FILE *fp = fopen( inFileName.c_str(), "w" );
00062 if( fp )
00063 {
00064 if( mStream && mStream != stdout )
00065 {
00066 fclose( mStream );
00067 }
00068 mStream = fp;
00069 }
00070 }
00071 }
00072
00073 Logger::Logger()
00074 :mStream(stdout) {
00075 }
00076
00077 Logger::~Logger() {
00078 if( mStream && mStream != stdout )
00079 {
00080 fclose( mStream );
00081 mStream = NULL;
00082 }
00083 }
00084
00085 Logger *
00086 Logger::instance() {
00087 static Logger obj;
00088 return &obj;
00089 }
00090
00091 #endif //LOGGING
00092
00093 }
00094
00095 namespace torc {
00096 namespace generic {
00097 void
00098 openLogFile( const std::string &logFileName )
00099 {
00100 #ifdef LOGGING
00101 Logger::instance()->openLogFile( logFileName );
00102 #endif
00103 return;
00104 }
00105
00106 void
00107 log( const char *fmt, ... )
00108 {
00109 #ifdef LOGGING
00110 va_list args;
00111 va_start( args, fmt );
00112 Logger::instance()->write( fmt, args );
00113 va_end( args );
00114 #endif
00115 return;
00116 }
00117
00118 }
00119 }
00120