Cadabra
Computer algebra system for field theory problems
Snoop.hh
Go to the documentation of this file.
1 
2 /*
3 
4  Snoop
5 Copyright (C) 2015-2016 Kasper Peeters
6 Available under the terms of the GPL v3.
7 
8  Snoop is a lightweight logging library which stores its log entries in
9 a local SQLite database or on a remote server.
10 
11 */
12 
13 #pragma once
14 
15 #include <string>
16 #include <sstream>
17 #include <sqlite3.h>
18 #include <stdint.h>
19 #include <mutex>
20 #include <json/json.h>
21 #include <thread>
22 
23 #ifndef _MSC_VER
24 #include <unistd.h>
25 #endif
26 
27 namespace snoop {
28 
29  class SnoopImpl;
30  class Flush {};
31  extern Flush flush;
32 
35 
36  class Snoop {
37  public:
38  Snoop();
39  ~Snoop();
40 
44 
45  void init(const std::string& app_name, const std::string& app_version,
46  std::string server="", std::string local_log_file="");
47 
52 
53  std::string get_user_uuid(const std::string& app_name);
54 
58 
59  Snoop& operator()(const std::string& type, std::string fl="", int loc=-1, std::string method="");
60 
62 
63  template<class T>
64  Snoop& operator<<(const T& obj)
65  {
66  out_ <<(obj);
67  return *this;
68  }
69 
71 
72  Snoop& operator<<(const Flush&);
73 
75 
76  void set_sync_immediately(bool);
77 
82 
83  void sync_with_server(bool from_wsthread=false);
84 
86 
87  void sync_runs_with_server(bool from_wsthread=false);
88 
90 
91  void sync_logs_with_server(bool from_wsthread=false);
92 
93 
95 
96  class AppEntry {
97  public:
98  AppEntry();
99  AppEntry(const std::string& uuid_, uint64_t create_millis_, uint64_t receive_millis_, uint64_t pid_,
100  const std::string& ip_address_, const std::string& machine_id_,
101  const std::string& app_name_, const std::string& app_version_,
102  const std::string& user_id_,
103  int server_status_);
104 
105  std::string to_json(bool human_readable) const;
106  void from_json(const Json::Value&);
107 
108  int id;
109  std::string uuid;
110  uint64_t create_millis;
111  uint64_t receive_millis;
112  uint64_t pid;
113  std::string ip_address;
114  std::string machine_id;
115  std::string app_name;
116  std::string app_version;
117  std::string user_id;
118  int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
119  bool connected;
120  };
121 
123 
124  class LogEntry {
125  public:
126  LogEntry();
127  LogEntry(int log_id_, int client_log_id_, int id_, const std::string&,
128  uint64_t, uint64_t, const std::string&, int, const std::string&,
129  const std::string&, const std::string&, int status);
130 
131  std::string to_json(bool human_readable) const;
132  void from_json(const Json::Value&);
133 
134  int log_id;
136  int id;
137  std::string uuid;
138  uint64_t create_millis;
139  uint64_t receive_millis;
140  std::string loc_file;
141  int loc_line;
142  std::string loc_method;
143  std::string type;
144  std::string message;
145  int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
146  };
147 
148  protected:
149  std::ostringstream out_;
151 
153  friend SnoopImpl;
154 
155  };
156 
157  extern Snoop log;
158 
159  const char info[] ="info";
160  const char warn[] ="warning";
161  const char error[]="error";
162  const char fatal[]="fatal";
163  const char email[]="email";
164  }
165 
166 // set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst
167 // ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
168 
169 #define LOC __FILE__, __LINE__, __func__
170 
snoop::Snoop::SnoopImpl
friend SnoopImpl
Definition: Snoop.hh:153
snoop::Snoop::LogEntry
C++ representation of a log entry.
Definition: Snoop.hh:124
snoop::Snoop::LogEntry::uuid
std::string uuid
Definition: Snoop.hh:137
snoop::Snoop::~Snoop
~Snoop()
Definition: Snoop.cc:812
snoop::Snoop::AppEntry::pid
uint64_t pid
Definition: Snoop.hh:112
snoop::Snoop::sync_runs_with_server
void sync_runs_with_server(bool from_wsthread=false)
As above, but only for run entries.
Definition: Snoop.cc:498
snoop::log
Snoop log
Definition: Snoop.cc:48
snoop::Snoop::AppEntry::id
int id
Definition: Snoop.hh:108
snoop::Snoop::Snoop
Snoop()
Definition: Snoop.cc:63
snoop::Snoop::LogEntry::receive_millis
uint64_t receive_millis
Definition: Snoop.hh:139
snoop::Snoop::operator()
Snoop & operator()(const std::string &type, std::string fl="", int loc=-1, std::string method="")
Operator to initialise a logging entry with the type of the log message as well as (optionally) the f...
Definition: Snoop.cc:852
snoop::Snoop::LogEntry::log_id
int log_id
Definition: Snoop.hh:134
snoop::Snoop::AppEntry
C++ representation of a run entry.
Definition: Snoop.hh:96
snoop::Snoop::AppEntry::create_millis
uint64_t create_millis
Definition: Snoop.hh:110
snoop::Snoop::sync_with_server
void sync_with_server(bool from_wsthread=false)
Ensure that the local database is synchronised with the server (this sends multiple app or log entrie...
Definition: Snoop.cc:481
snoop::Snoop::operator<<
Snoop & operator<<(const T &obj)
Generic operator to log an object to the log message being constructed.
Definition: Snoop.hh:64
snoop::Snoop::out_
std::ostringstream out_
Definition: Snoop.hh:149
snoop::Snoop::LogEntry::LogEntry
LogEntry()
Definition: Snoop.cc:905
snoop::Snoop::LogEntry::client_log_id
int client_log_id
Definition: Snoop.hh:135
snoop::Snoop
Logging class with functionality to send log information to a remote server using a websocket connect...
Definition: Snoop.hh:36
snoop::Snoop::sync_logs_with_server
void sync_logs_with_server(bool from_wsthread=false)
As above, but only for log entries.
Definition: Snoop.cc:580
snoop::Snoop::AppEntry::machine_id
std::string machine_id
Definition: Snoop.hh:114
snoop::Snoop::AppEntry::connected
bool connected
Definition: Snoop.hh:119
snoop::Snoop::AppEntry::user_id
std::string user_id
Definition: Snoop.hh:117
snoop::Snoop::AppEntry::app_version
std::string app_version
Definition: Snoop.hh:116
snoop::email
const char email[]
Definition: Snoop.hh:163
snoop::Snoop::AppEntry::uuid
std::string uuid
Definition: Snoop.hh:109
snoop::Snoop::set_sync_immediately
void set_sync_immediately(bool)
Set to sync with server after every log line.
Definition: Snoop.cc:222
snoop::Snoop::LogEntry::loc_method
std::string loc_method
Definition: Snoop.hh:142
snoop::Snoop::AppEntry::server_status
int server_status
Definition: Snoop.hh:118
snoop::Snoop::LogEntry::server_status
int server_status
Definition: Snoop.hh:145
snoop::Flush
Definition: Snoop.hh:30
snoop::Snoop::get_user_uuid
std::string get_user_uuid(const std::string &app_name)
Get a string which uniquely identifies the current user.
Definition: Snoop.cc:168
snoop::info
const char info[]
Definition: Snoop.hh:159
snoop::Snoop::LogEntry::message
std::string message
Definition: Snoop.hh:144
snoop::Snoop::LogEntry::type
std::string type
Definition: Snoop.hh:143
snoop::Snoop::LogEntry::to_json
std::string to_json(bool human_readable) const
Definition: Snoop.cc:919
snoop::warn
const char warn[]
Definition: Snoop.hh:160
snoop::Snoop::impl
SnoopImpl * impl
Definition: Snoop.hh:152
snoop::Snoop::sync_immediately_
bool sync_immediately_
Definition: Snoop.hh:150
snoop::Snoop::AppEntry::receive_millis
uint64_t receive_millis
Definition: Snoop.hh:111
snoop::Snoop::AppEntry::AppEntry
AppEntry()
Definition: Snoop.cc:957
snoop::Snoop::AppEntry::ip_address
std::string ip_address
Definition: Snoop.hh:113
snoop::Snoop::AppEntry::to_json
std::string to_json(bool human_readable) const
Definition: Snoop.cc:973
snoop::flush
Flush flush
Definition: Snoop.cc:49
snoop::Snoop::LogEntry::loc_file
std::string loc_file
Definition: Snoop.hh:140
snoop::Snoop::AppEntry::from_json
void from_json(const Json::Value &)
Definition: Snoop.cc:1022
snoop::SnoopImpl
Definition: SnoopPrivate.hh:17
snoop::Snoop::LogEntry::from_json
void from_json(const Json::Value &)
Definition: Snoop.cc:1007
snoop::fatal
const char fatal[]
Definition: Snoop.hh:162
snoop::Snoop::LogEntry::create_millis
uint64_t create_millis
Definition: Snoop.hh:138
cadabra2_defaults.server
server
Definition: cadabra2_defaults.py:153
snoop::Snoop::AppEntry::app_name
std::string app_name
Definition: Snoop.hh:115
snoop::error
const char error[]
Definition: Snoop.hh:161
snoop::Snoop::LogEntry::id
int id
Definition: Snoop.hh:136
snoop::Snoop::LogEntry::loc_line
int loc_line
Definition: Snoop.hh:141
snoop::Snoop::init
void init(const std::string &app_name, const std::string &app_version, std::string server="", std::string local_log_file="")
Initialise the logging stream.
Definition: Snoop.cc:73
snoop
Definition: Snoop.hh:27