TextLog.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef UTIL_TEXTLOG_C_DEFINED
00039 #define UTIL_TEXTLOG_C_DEFINED
00040
00041 #include "Util/TextLog.H"
00042
00043 #include "Util/Assert.H"
00044 #include "Util/SimTime.H"
00045 #include "Util/log.H"
00046 #include "Util/sformat.H"
00047 #include "rutz/mutex.h"
00048
00049 #include <fstream>
00050 #include <map>
00051
00052
00053
00054
00055
00056
00057
00058
00059 namespace
00060 {
00061
00062 class TextLogger
00063 {
00064 public:
00065 TextLogger(const std::string& fname)
00066 :
00067 itsMutex(),
00068 itsStream(fname.c_str()),
00069 itsTime(SimTime::ZERO())
00070 {
00071 pthread_mutex_init(&itsMutex, 0);
00072
00073 if (!itsStream.is_open())
00074 {
00075 LFATAL("Couldn't open text log file '%s' for writing",
00076 fname.c_str());
00077 }
00078 else
00079 {
00080 LINFO("Opened textlog file '%s'", fname.c_str());
00081 }
00082 }
00083
00084 void log(const std::string& event,
00085 const std::string& details)
00086 {
00087 this->log(event, details, itsTime);
00088 }
00089
00090 void log(const std::string& event,
00091 const std::string& details,
00092 const SimTime& t)
00093 {
00094 GVX_MUTEX_LOCK(&itsMutex);
00095
00096 itsStream
00097 << sformat("%010.02fms %-20s ", t.msecs(), event.c_str())
00098 << details << std::endl;
00099 itsStream.flush();
00100 }
00101
00102 void setTime(const SimTime& t)
00103 {
00104 GVX_MUTEX_LOCK(&itsMutex);
00105
00106 itsTime = t;
00107 }
00108
00109 private:
00110 pthread_mutex_t itsMutex;
00111 std::ofstream itsStream;
00112 SimTime itsTime;
00113 };
00114
00115
00116 typedef std::map<std::string, TextLogger*> LoggerMap;
00117
00118 LoggerMap* g_loggers;
00119 pthread_mutex_t g_loggers_mutex = PTHREAD_MUTEX_INITIALIZER;
00120
00121 pthread_once_t g_loggers_init_once = PTHREAD_ONCE_INIT;
00122 void loggers_init()
00123 {
00124 ASSERT(g_loggers == 0);
00125 g_loggers = new LoggerMap;
00126 }
00127
00128
00129 TextLogger* get_logger(const std::string& fname)
00130 {
00131 pthread_once(&g_loggers_init_once, loggers_init);
00132
00133 ASSERT(g_loggers != 0);
00134
00135 GVX_MUTEX_LOCK(&g_loggers_mutex);
00136
00137 LoggerMap::iterator itr = g_loggers->find(fname);
00138
00139 if (itr != g_loggers->end())
00140 {
00141 return (*itr).second;
00142 }
00143
00144
00145 TextLogger* logger = new TextLogger(fname);
00146 g_loggers->insert(LoggerMap::value_type(fname, logger));
00147 return logger;
00148 }
00149 }
00150
00151
00152 void textLog(const std::string& fname,
00153 const std::string& event,
00154 const std::string& details)
00155 {
00156 if (!fname.empty())
00157 get_logger(fname)->log(event, details);
00158 }
00159
00160
00161 void textLog(const std::string& fname,
00162 const std::string& event,
00163 const std::string& details,
00164 const SimTime& t)
00165 {
00166 if (!fname.empty())
00167 get_logger(fname)->log(event, details, t);
00168 }
00169
00170
00171 void setLogTime(const std::string& fname,
00172 const SimTime& t)
00173 {
00174 if (!fname.empty())
00175 get_logger(fname)->setTime(t);
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185 #endif // UTIL_TEXTLOG_C_DEFINED