1#ifndef _DRAMA2_LOGGER_INC
2#define _DRAMA2_LOGGER_INC
47#if defined(__GNUC__) && !defined(__clang__)
48#define _DRAMA_HaveGnuFileBufExt
49#define _DRAMA_GnuFileBufExtOnly(_A_) _A_
50#include <ext/stdio_filebuf.h>
52#define _DRAMA_GnuFileBufExtOnly(_A_) _A_
65#define D2LOG_ALL 0xFFFFFFFF
66#define D2LOG_STARTUP 0x01
67#define D2LOG_ERRORS 0x02
68#define D2LOG_ACTENT 0x04
69#define D2LOG_ACTEXIT 0x08
70#define D2LOG_INST 0x10
72#define D2LOG_DEBUG 0x40
73#define D2LOG_COMMS 0x80
74#define D2LOG_USER1 0x100
75#define D2LOG_USER2 0x200
76#define D2LOG_USER3 0x400
77#define D2LOG_USER4 0x800
78#define D2LOG_ARGS 0x1000
80#define D2LOG_DRAMA2 0x2000
82#define D2LOG_ALWAYS 0x40000
83#define D2LOG_ALWAYS_S 0x80000
96 typedef std::map<std::thread::id, std::pair<std::string, Dits___CurActType>> DramaCtxMapType;
97 typedef DramaCtxMapType::iterator DramaCtxMapIter;
98 typedef DramaCtxMapType::const_iterator DramaCtxMapIterConst;
120 static long int _s_userTimeOffset;
156 const char *TimeString()
const {
return _timeStr; };
161 long GetTime()
const {
return _LastTime; };
165 int Millisec()
const {
return _millisec; };
169 static void SetTimeOffset(
long int t) { _s_userTimeOffset =
t; }
170 static long int GetTimeOffset() {
return _s_userTimeOffset; }
199 class LogFileStream :
public std::ofstream {
201#ifdef _DRAMA_HaveGnuFileBufExt
206 std::unique_ptr<__gnu_cxx::stdio_filebuf<char> >
_fileBuf;
221#ifdef _DRAMA_HaveGnuFileBufExt
228 void fopen(
const std::string &
filename);
241#ifdef _DRAMA_HaveGnuFileBufExt
258 "Attempt to call LogBuffer::rdbuf");
281 class GrabLogContext {
282 bool _onKnownThread =
false;
283 bool _onCreateThread =
false;
284 bool _onRunThread =
false;
288 GrabLogContext(
const Logger &);
291 bool IsKnownThread()
const {
292 return _onKnownThread;
295 bool IsDramaCreateThread()
const {
296 return _onCreateThread;
299 bool IsDramaRunThread()
const {
303 std::string FuncName()
const {
307 return "<<::RunDrama>>";
311 return "<<ukn thd fnc>>";
352 class LogStreamBuf :
public std::streambuf {
361 static const int BUFFER_SIZE = 200;
362 char _buffer[BUFFER_SIZE+1];
381 if (flushBuffer() ==
EOF)
391 virtual int sync()
override {
392 if (flushBuffer() ==
EOF) {
414 setp(_buffer, _buffer+(BUFFER_SIZE-1));
434 fprintf(
stderr,
"DRAMA Exception thrown by drama::~LogStreamBuf()\n");
437 static_cast<long int>(
e.dramaStatus()),
438 e.dramaStatusStr().c_str());
450 LogStreamBuf(
const LogStreamBuf &
source) =
delete;
469 _theLogger(
source._theLogger),
472 source._theLogger =
nullptr;
485 this->_theLogger =
rhs._theLogger;
486 this->_level =
rhs._level;
487 this->_prefix = std::move(
rhs._prefix);
489 rhs._theLogger =
nullptr;
497#ifdef D2_PROVIDE_LOG_STREAM
543 LogStream(
Logger *theLogger,
unsigned level,
const std::string &prefix) :
544 _buf(theLogger, level, prefix) {
558 LogStream(
const LogStream &source) =
delete;
567 LogStream& operator = (
const LogStream &rhs) =
delete;
573 LogStream(LogStream &&source) noexcept : _buf(std::move(source._buf)){ }
580 LogStream& operator=(LogStream &&rhs)
noexcept {
582 _buf = std::move(rhs._buf);
744 friend class GrabLogContext;
754 int minsig;
unsigned level;
const char *string;
758 static const LevelStruct _s_LevelNames[];
759 static const char *_s_RescheduleReasons[];
760 static const char *_s_Requests[];
761 static const char *_s_PathStatus[];
762 static const char *_s_MessageTypes[];
765 static const int _s_MsgLen;
768 unsigned _currentLevels =
776 bool _disableMsgOut =
false;
779 LogFileStream _logFile;
781 std::string _logFileName;
794 Task *
const _theTask;
798 std::string _directory;
803 bool _flushAlways =
false;
807 bool _noFsync =
false;
811 bool _noLogFlush =
false;
816 bool _flushed =
false;
821 bool _failed =
false;
825 bool _compress =
false;
826 FILE *_compressFD =
nullptr;
843 bool _logThreadID =
false;
849 DramaCtxMapType _threadToDRAMAContextMap;
854 std::thread::id _mainThreadId;
873 void LogTaskVersionInfo();
880 static DVOID ActionReturnLog(
889 static DVOID GetPathLog(
898 static DVOID MsgSendLog(
905 static DVOID LoadLog(
914 static DVOID NotifyReqLog(
920 static DVOID MsgOutLog(
924 static DVOID LogMsgLog(
932 static DVOID LogTrigger(
937 static DVOID LogTrigBulk(
945 static DVOID LogSignal(
952 static DVOID LogBulkSend(
965 static DVOID LogShutdown(
979 void LogMessage(
const char *
prefix,
990 std::string GetLevels()
const;
993 void LevelCheckAndAdd(
unsigned index, std::string *
s)
const;
998 void CheckFileSize();
1001 void CheckCompressMode();
1003 void CloseLogFile();
1009 static bool NameUsed(
const std::string &name);
1014 void CheckDayChange(
int newDay);
1020 void operator=(
const Logger&) =
delete;
1034 bool _previousState;
1038 _previousState(
theLogger->_disableMsgOut) {
1042 _theLogger->_disableMsgOut = _previousState;;
1082 const std::string &
dir=
"",
1124 const char *
fmt, ...)
1151 template<
typename...
Types>
1152 void SLog(
unsigned level,
const std::string &
prefix,
1158 std::stringstream
sstrm;
1192 template<
typename...
Types>
1193 void SLog( std::ostream &ostream,
1194 unsigned level,
const std::string &
prefix,
1200 std::stringstream
sstrm;
1204 ostream <<
sstrm.str() << std::endl;
1235 const char *
fmt, ...)
1277 std::thread::id
threadId=std::this_thread::get_id(),
1278 const char*
func =
"*unnamed func*");
1299 const char*
func =
"*unamed func*");
1327 const char*
func =
"*unnamed func*");
1346 const char*
func =
"*non-drama*");
1350#ifdef D2_PROVIDE_LOG_STREAM
1372 const std::string &
prefix) {
An Exception class for exceptions thrown by DRAMA V2 classes.
Definition exception.hh:164
A class which implements a DRAMA task.
Definition task.hh:441
LogStreamBuf & operator=(LogStreamBuf &&rhs) noexcept
Move assignment operator.
Definition logger.hh:510
LogStreamBuf(Logger *theLogger, unsigned level, const std::string &prefix)
Construct a LogStreamBuf object.
Definition logger.hh:432
LogStreamBuf(const LogStreamBuf &source)=delete
Copy constructor - deleted.
virtual ~LogStreamBuf()
Destroy the LogStreamBuf, any remaining output is sent.
Definition logger.hh:447
LogStreamBuf(LogStreamBuf &&source) noexcept
Move copy constructor.
Definition logger.hh:495
LogStreamBuf & operator=(const LogStreamBuf &rhs)=delete
Assignment operator - deleted.
Implement a streambuf sub-class that can write messages via the DRAMA Logger interface.
Definition logger.hh:379
void Set(unsigned newLevels)
Enable new logging levels.
void RegisterThread(const Dits___CurActType &dramaContext, std::thread::id threadId=std::this_thread::get_id(), const char *func="*unnamed func*")
Register the DRAMA context of a thread.
void RegisterThread(std::thread::id threadId=std::this_thread::get_id(), const char *func="*unamed func*")
Register the current DRAMA context against a thread.
void RegisterThread(std::thread::id sourceThread, std::thread::id newThread, const char *func="*unnamed func*")
Register the DRAMA context of one thread against a new thread.
static void SetTimeOffset(long int t)
Change the time offset.
Definition logger.hh:1133
void SLog(std::ostream &ostream, unsigned level, const std::string &prefix, const std::string &fmt, Types... args)
Log a message using type safe formatting and output to a stream.
Definition logger.hh:1220
void SLog(unsigned level, const std::string &prefix, const std::string &fmt, Types... args)
Log a message using type safe formatting.
Definition logger.hh:1179
Logger(Task *dramaTask)
Construct a drama::logging::Logger object.
void Open(const std::string &system, const std::string &dir="", bool compress=false)
Open the log file.
void Log(const IMP_AbsTime *time, unsigned level, bool nofmt, const char *prefix, const char *fmt,...)
Log a message with a specified event time.
void Unset(unsigned disableLevels)
Clear particular logging levels.
void Log(unsigned level, bool nofmt, const char *prefix, const char *fmt,...)
Log a message using C printf formating.
void RegisterThreadNoDramaCtx(std::weak_ptr< Task > dramaTask, const char *func="*non-drama*")
Register this thread with with the logger, but without a DRAMA context.
Implementation of a Class supporting Logging within an AAO DRAMA Task.
Definition logger.hh:769
#define DramaTHROW(status_, message_)
Throw a Drama exception.
Definition exception.hh:93
#define D2LOG_ALWAYS
Always log the message.
Definition logger.hh:109
#define D2LOG_ALWAYS_S
Always log the messages, including to the screen.
Definition logger.hh:110
#define D2LOG_ERRORS
Log Ers and action completion errors.
Definition logger.hh:94
#define D2LOG_MSG
Log MsgOut messages.
Definition logger.hh:98
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1322
void SafePrintf(std::ostream &ostream, const char *str)
Safe formatted write to a stream.
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:93