00001
00002 #ifndef __FesaLogger__H__
00003 #define __FesaLogger__H__
00004
00005 #include <unistd.h>
00006 #include <stdio.h>
00007 #include <string>
00008 #include <fstream>
00009 #include <iomanip>
00010 #include <signal.h>
00011
00012 #include <fesa-core/Diagnostic/TimeStampCounter.h>
00013 #include <fesa-core/Diagnostic/UdpLogger.h>
00014 #include <fesa-core/Utilities/Mutex.h>
00015 #include <fesa-core/DataStore/AbstractDevice.h>
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #define LOGERROR(severity) pLog->logError(fesa::FesaLogger::severity)
00041 #define FWK_LOGERROR(severity) pLog->logError(FesaLogger::severity, FesaLogger::FWK_SENDER)
00042
00043
00044 #define LOGTRACE(topic) if (pLog->isEnabledFor(topic)) *pLog
00045 #define FWK_LOGTRACE(topic) if (pLog->isEnabledFor(FesaLogger::topic)) *pLog
00046
00047
00048 #define LOGTRACE_DEVICE(topic, pdev) if (pLog->isEnabledFor(topic, pdev)) *pLog
00049 #define FWK_LOGTRACE_DEVICE(topic, pdev) if (pLog->isEnabledFor(FesaLogger::topic, pdev)) *pLog
00050
00051 namespace fesa
00052 {
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 const std::string defaultLogName = "DLT";
00063 const std::string rtLogName = "RT";
00064 const std::string srvLogName = "SRV";
00065 const std::string evtLogName = "EVT";
00066 const std::string notifLogName = "NOTIF";
00067 const std::string bgrNotifLogName = "BGR_NOTIF";
00068 const std::string rdaLogName = "RDA";
00069 const std::string persLogName = "PSTCY";
00070
00071 const std::string undefinedClassName = std::string("-");
00072 const std::string undefinedThreadName = std::string("-");
00073 const std::string undefinedFunctionName = std::string("-");
00074
00075 class FesaLoggerBuffer;
00076
00077
00078
00079 class FesaLogger: public std::ostream
00080 {
00081
00082 public:
00083
00084
00085
00086
00087 static const std::string fwkSender;
00088 static const std::string usrSender;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 enum DIAG_FWK_TOPIC
00103 {
00104 getActionProfiling = 1 << 0,
00105 setActionProfiling = 1 << 1,
00106 rtActionProfiling = 1 << 2,
00107 eventProfiling = 1 << 3,
00108 notificationProfiling = 1 << 4,
00109 getActionTracking = 1 << 5,
00110 setActionTracking = 1 << 6,
00111 rtActionTracking = 1 << 7,
00112 eventTracking = 1 << 8,
00113 notificationTracking = 1 << 9,
00114 persistencyTracking = 1 << 10,
00115 transactionTracking = 1 << 11,
00116 subscriptionTracking = 1 << 12,
00117 signalHandlerTracking = 1 << 13,
00118 rdaTracking = 1 << 15,
00119 startUpTracking = 1 << 31
00120 };
00121
00122
00123
00124
00125
00126
00127 enum LogType
00128 {
00129 errorType = 0,
00130 traceType = 1
00131 };
00132
00133
00134
00135
00136
00137
00138 enum LogSeverity
00139 {
00140
00141
00142 fatal = 0,
00143 alert = 1,
00144 critical = 2,
00145 error = 3,
00146 warning = 4,
00147 notice = 5,
00148 none = 6,
00149 debug = 7
00150 };
00151
00152
00153
00154 enum LogAction
00155 {
00156 undefined = 0,
00157 start = 1,
00158 stop = 2
00159 };
00160
00161
00162 inline FesaLogger & logError(LogSeverity severity, const std::string& sender);
00163
00164
00165 inline bool isEnabledFor(int32_t topic, const std::string& name, LogAction action);
00166
00167
00168 inline bool isEnabledFor(DIAG_FWK_TOPIC topic);
00169
00170
00171 inline bool isEnabledFor(int32_t topic, AbstractDevice* pdev, const std::string& name, LogAction action);
00172
00173 static bool isEnabled();
00174
00178 FesaLogger(const FesaLogger&);
00179
00183 ~FesaLogger();
00184
00185 static void init(char *bname);
00186 static void enableConsoleOutput();
00187 static void disableConsoleOutput();
00188 static void reinit();
00189 static void destroy();
00190
00191
00192
00193
00194
00195
00196
00197 static FesaLogger* getLogger(const std::string& source, const std::string& thread, const std::string& cname);
00198
00199
00200 static FesaLogger* getLogger(const std::string& source, const std::string& thread);
00201
00202
00203 static FesaLogger* getDefaultLogger();
00204
00205
00206 static void setDiagnosticMode(bool mode);
00207
00208
00209 static void setCustomTopic(int32_t customTopic, const std::string& cname);
00210
00211
00212 void send(const char* msg, LogType type);
00213
00214
00215 static int32_t fwkTopic_;
00216 int32_t customTopic_;
00217
00218
00219 static bool modeDiagnostic_;
00220
00221
00222 static uint32_t logCount_;
00223
00224
00225 static TsCounter logTsc_;
00226
00227
00228
00229
00230
00231
00232
00233 static std::string connectedHostName_;
00234 static int32_t connectedPortNumber_;
00235
00236 std::string className_;
00237 std::string logSource_;
00238 std::string logThread_;
00239 std::string logName_;
00240
00241
00242 private:
00243
00244 friend class FesaLoggerBuffer;
00245
00246 FesaLogger(FesaLoggerBuffer* pBuf);
00247
00248
00249
00250
00251 static UdpLogger* pUDPLog_;
00252
00253
00254
00255
00256
00257 LogSeverity errSeverity_;
00258
00259
00260 LogType logType_;
00261
00262
00263
00264 int32_t lastError_;
00265
00266
00267 static bool isInitPhase_;
00268
00269 static FesaLogger* errLog_;
00270
00271
00272
00273
00274 static const char* logActionStr_[];
00275
00276
00277 static std::map<std::string, FesaLogger *> logFactoryMap_;
00278
00279
00280 static FesaLogger* defaultLog_;
00281
00282
00283 static Mutex mutex_;
00284
00285
00286 FesaLoggerBuffer* logBuffer_;
00287 };
00288
00289
00290
00291 class FesaLoggerBuffer: public std::streambuf
00292 {
00293 public:
00294 FesaLoggerBuffer()
00295 {
00296 setp(buffer_, buffer_ + (bufferSize_ - 1));
00297 }
00298
00299 void setFesaLogger(FesaLogger* pLogger)
00300 {
00301 logger_ = pLogger;
00302 }
00303 ;
00304
00305 protected:
00306 virtual int32_t sync();
00307 virtual int32_t overflow(int32_t);
00308
00309 private:
00310
00311
00312
00313 static const int32_t bufferSize_ = 1479;
00314 char buffer_[bufferSize_];
00315
00316 FesaLogger* logger_;
00317 };
00318
00322 inline FesaLogger* FesaLogger::getDefaultLogger()
00323 {
00324 return defaultLog_;
00325 }
00326
00327 #define SET_TRACE_HEADER(sender) \
00328 logType_ = traceType; \
00329 errSeverity_ = none; \
00330 *this << sender << "|TRACE|" << logSource_ << className_ << logThread_ << logCount_++ << "|" << std::setprecision(20) << logTsc_.getValue(YS_UNIT) << "|"
00331
00332 inline FesaLogger & FesaLogger::logError(LogSeverity severity, const std::string& sender = FesaLogger::usrSender)
00333 {
00334 logType_ = errorType;
00335 errSeverity_ = severity;
00336 *this << sender << "|ERROR|";
00337 return *this;
00338 }
00339
00340 inline bool FesaLogger::isEnabledFor(int32_t topic, const std::string& name = undefinedFunctionName, LogAction action =
00341 undefined)
00342 {
00343
00344 if (modeDiagnostic_ && (topic & customTopic_))
00345 {
00346
00347
00348 SET_TRACE_HEADER("USR") << name << logActionStr_[action];
00349 return true;
00350 }
00351 return false;
00352 }
00353
00354 inline bool FesaLogger::isEnabledFor(DIAG_FWK_TOPIC topic)
00355 {
00356
00357 if (modeDiagnostic_ && (topic & fwkTopic_))
00358 {
00359
00360 SET_TRACE_HEADER("FWK");
00361 return true;
00362 }
00363 return false;
00364 }
00365
00366 inline bool FesaLogger::isEnabledFor(int32_t topic, AbstractDevice* pdev, const std::string& name =
00367 undefinedFunctionName, LogAction action = undefined)
00368 {
00369 if (modeDiagnostic_ && (topic & customTopic_) && pdev->isLoggable_)
00370 {
00371
00372
00373 SET_TRACE_HEADER("USR") << name << logActionStr_[action];
00374 return true;
00375 }
00376 return false;
00377 }
00378
00379 }
00380
00381 #endif // __FesaLogger__H__