plebble
task.h
Go to the documentation of this file.
1 /*
2 -------------------------------------------------------------------------------
3  PLEBBLE
4 -------------------------------------------------------------------------------
5  Copyright (C) 2019-2020 KATLAS Technology. (http://katlastechnology.com)
6  Copyright (C) 2017-2020 Marcos Mayorga. (mm@mm-studios.com)
7 
8  This file is part of our Plebble(R) Platform.
9 
10  The code below cannot be copied, used for any purpose other than the one
11  agreed and/or distributed without the express permission of
12  KATLAS Technology.
13 -------------------------------------------------------------------------------
14 
15 
16 */
17 #ifndef USGOV_cc4a6c5e5e2c6e084fa5b97911783920c4c9c1fc5b484939dbc7448e7ebcb559
18 #define USGOV_cc4a6c5e5e2c6e084fa5b97911783920c4c9c1fc5b484939dbc7448e7ebcb559
19 
20 #include <unordered_map>
21 #include <string>
22 #include <cassert>
23 #include <thread>
24 #include <mutex>
25 #include <vector>
26 #include <fstream>
27 #include <chrono>
28 #include <sstream>
29 #include <sys/types.h>
30 #include <atomic>
31 #include <unistd.h>
32 #include <iostream>
33 
34 namespace us { namespace dbg {
35  using namespace std;
36  using namespace chrono;
37 
38  struct thread_logger;
39 
40  struct task final {
42  ~task();
43 
44  void start(const string&dir, const string&name);
45  static bool start(ostream*&, const thread_logger&, const string& dir, const string&name, short g);
46 
47  template<typename type1>
48  void dolog(const type1& arg1) {
49  *logfile << arg1 << ' ';
50  if (teecout) std::cout << arg1 << ' ';
51  }
52 
53  template<typename type1, typename... types>
54  void dolog(const type1& arg1, const types& ... args) {
55  dolog(arg1);
56  dolog(args...);
57  }
58 
59  static void prefix(ostream&, const thread_logger&, const string&log_level_, const string& log_class_);
60 
61  template<typename... Args>
62  void log(const string&log_level, const string& log_class, const Args&... args) {
63  if (onduty) return; //operator << can trigger recursive log calls which we ignore
64  if (logfile==0) start("","unknown");
65  onduty=true;
66  prefix(*logfile,logger,log_level,log_class);
67  if (teecout) prefix(std::cout,logger,log_level,log_class);
68  dolog(args...);
69  onduty=false;
70  *logfile << endl;
71  if (teecout) std::cout << endl;
72  }
73 
74  template<typename O>
75  void logdump(const string&log_level, const string&log_class, const O& o) {
76  if (onduty) return;
77  ostringstream pfx;
78  prefix(pfx,logger,log_level,log_class);
79  o.dump(pfx.str(),*logfile);
80  if (teecout) {
81  o.dump(pfx.str(),std::cout);
82  }
83  }
84 
85  template<typename O>
86  void logdump(const string&log_level, const string&log_class, const string&logpfx, const O& o) {
87  if (onduty) return;
88  ostringstream pfx;
89  prefix(pfx,logger,log_level,log_class);
90  string spfx=pfx.str()+logpfx;
91  o.dump(spfx,*logfile);
92  if (teecout) {
93  o.dump(spfx,std::cout);
94  }
95  }
96 
97  bool onduty{false};
98  void log_stacktrace() const;
99  static bool verbose; //false (def): logs go to file; true: logs go to cout
100  ostream* logfile{0};
101  bool ownlogfile{true};
103  static string pname;
104  static bool teecout;
105  static string logdir;
106  short g{0};
107  };
108 
109  template<>
110  void task::dolog(const vector<unsigned char>& arg1);
111 
112 }}
113 
114 #endif
115 
us::dbg::task::logdump
void logdump(const string &log_level, const string &log_class, const O &o)
Definition: task.h:75
us::dbg::task::teecout
static bool teecout
Definition: task.h:104
task.h
us::dbg::task::prefix
static void prefix(ostream &, const thread_logger &, const string &log_level_, const string &log_class_)
c
us::dbg::task c
Definition: task.cpp:23
thread_logger.h
us::dbg::task::start
static bool start(ostream *&, const thread_logger &, const string &dir, const string &name, short g)
us
Definition: daemon.h:22
log_stacktrace
#define log_stacktrace
Definition: gov.h:87
us::dbg::task::dolog
void dolog(const type1 &arg1, const types &... args)
Definition: task.h:54
us::dbg::task::pname
static string pname
Definition: task.h:103
us::dbg::task::dolog
void dolog(const type1 &arg1)
Definition: task.h:48
us::dbg::task::logdir
static string logdir
Definition: task.h:105
us::dbg::task::task
task(thread_logger &l)
us::dbg
Definition: task.h:34
us::dbg::thread_logger::ts
string ts() const
us::dbg::task::log
void log(const string &log_level, const string &log_class, const Args &... args)
Definition: task.h:62
us::dbg::task
Definition: task.h:40
us::dbg::task::start
void start(const string &dir, const string &name)
us::dbg::task::log_stacktrace
void log_stacktrace() const
us::dbg::thread_logger
Definition: thread_logger.h:38
std
Definition: app.h:380
stacktrace.h
config.h
us::dbg::task::logger
const thread_logger & logger
Definition: task.h:102
us::dbg::task::logdump
void logdump(const string &log_level, const string &log_class, const string &logpfx, const O &o)
Definition: task.h:86
us::dbg::task::verbose
static bool verbose
Definition: task.h:99
us::dbg::task::~task
~task()
LOGDIR
#define LOGDIR
Definition: config.h:12