Cadabra
Computer algebra system for field theory problems
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 namespace snoop {
24 
25  class SnoopImpl;
26  class Flush {};
27  extern Flush flush;
28 
31 
32  class Snoop {
33  public:
34  Snoop();
35  ~Snoop();
36 
40 
41  void init(const std::string& app_name, const std::string& app_version,
42  std::string server="", std::string local_log_file="");
43 
48 
49  std::string get_user_uuid(const std::string& app_name);
50 
54 
55  Snoop& operator()(const std::string& type, std::string fl="", int loc=-1, std::string method="");
56 
58 
59  template<class T>
60  Snoop& operator<<(const T& obj) {
61  out_ <<(obj);
62  return *this;
63  }
64 
66 
67  Snoop& operator<<(const Flush&);
68 
70 
71  void set_sync_immediately(bool);
72 
77 
78  void sync_with_server(bool from_wsthread=false);
79 
81 
82  void sync_runs_with_server(bool from_wsthread=false);
83 
85 
86  void sync_logs_with_server(bool from_wsthread=false);
87 
88 
90 
91  class AppEntry {
92  public:
93  AppEntry();
94  AppEntry(const std::string& uuid_, uint64_t create_millis_, uint64_t receive_millis_, pid_t pid_,
95  const std::string& ip_address_, const std::string& machine_id_,
96  const std::string& app_name_, const std::string& app_version_,
97  const std::string& user_id_,
98  int server_status_);
99 
100  std::string to_json(bool human_readable) const;
101  void from_json(const Json::Value&);
102 
103  int id;
104  std::string uuid;
105  uint64_t create_millis;
106  uint64_t receive_millis;
107  pid_t pid;
108  std::string ip_address;
109  std::string machine_id;
110  std::string app_name;
111  std::string app_version;
112  std::string user_id;
113  int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
114  bool connected;
115  };
116 
118 
119  class LogEntry {
120  public:
121  LogEntry();
122  LogEntry(int log_id_, int client_log_id_, int id_, const std::string&,
123  uint64_t, uint64_t, const std::string&, int, const std::string&,
124  const std::string& , const std::string&, int status);
125 
126  std::string to_json(bool human_readable) const;
127  void from_json(const Json::Value&);
128 
129  int log_id;
131  int id;
132  std::string uuid;
133  uint64_t create_millis;
134  uint64_t receive_millis;
135  std::string loc_file;
136  int loc_line;
137  std::string loc_method;
138  std::string type;
139  std::string message;
140  int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
141  };
142 
143  protected:
144  std::ostringstream out_;
146 
148  friend SnoopImpl;
149 
150  };
151 
152  extern Snoop log;
153 
154  const char info[] ="info";
155  const char warn[] ="warning";
156  const char error[]="error";
157  const char fatal[]="fatal";
158  const char email[]="email";
159 }
160 
161 // set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst
162 // ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
163 
164 #define LOC __FILE__, __LINE__, __func__
165 
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:845
SnoopImpl * impl
Definition: Snoop.hh:147
std::ostringstream out_
Definition: Snoop.hh:144
std::string get_user_uuid(const std::string &app_name)
Get a string which uniquely identifies the current user.
Definition: Snoop.cc:162
std::string to_json(bool human_readable) const
Definition: Snoop.cc:912
int log_id
Definition: Snoop.hh:129
const char fatal[]
Definition: Snoop.hh:157
uint64_t receive_millis
Definition: Snoop.hh:106
C++ representation of an run entry.
Definition: Snoop.hh:91
Snoop & operator<<(const T &obj)
Generic operator to log an object to the log message being constructed.
Definition: Snoop.hh:60
std::string app_version
Definition: Snoop.hh:111
bool connected
Definition: Snoop.hh:114
int server_status
Definition: Snoop.hh:113
Definition: SnoopPrivate.hh:17
void from_json(const Json::Value &)
Definition: Snoop.cc:1000
const char email[]
Definition: Snoop.hh:158
uint64_t create_millis
Definition: Snoop.hh:133
uint64_t create_millis
Definition: Snoop.hh:105
int client_log_id
Definition: Snoop.hh:130
Logging class with functionality to send log information to a remote server using a websocket connect...
Definition: Snoop.hh:32
int id
Definition: Snoop.hh:131
std::string machine_id
Definition: Snoop.hh:109
std::string uuid
Definition: Snoop.hh:132
void set_sync_immediately(bool)
Set to sync with server after every log line.
Definition: Snoop.cc:216
int id
Definition: Snoop.hh:103
std::string user_id
Definition: Snoop.hh:112
const char error[]
Definition: Snoop.hh:156
std::string message
Definition: Snoop.hh:139
Snoop log
Definition: Snoop.cc:43
void from_json(const Json::Value &)
Definition: Snoop.cc:1015
Definition: Snoop.hh:26
Snoop()
Definition: Snoop.cc:57
void sync_logs_with_server(bool from_wsthread=false)
As above, but only for log entries.
Definition: Snoop.cc:573
bool sync_immediately_
Definition: Snoop.hh:145
int server_status
Definition: Snoop.hh:140
Flush flush
Definition: Snoop.cc:44
~Snoop()
Definition: Snoop.cc:805
const char warn[]
Definition: Snoop.hh:155
void sync_runs_with_server(bool from_wsthread=false)
As above, but only for run entries.
Definition: Snoop.cc:491
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:474
std::string loc_file
Definition: Snoop.hh:135
std::string type
Definition: Snoop.hh:138
const char info[]
Definition: Snoop.hh:154
std::string uuid
Definition: Snoop.hh:104
AppEntry()
Definition: Snoop.cc:950
LogEntry()
Definition: Snoop.cc:898
friend SnoopImpl
Definition: Snoop.hh:148
std::string to_json(bool human_readable) const
Definition: Snoop.cc:966
std::string ip_address
Definition: Snoop.hh:108
uint64_t receive_millis
Definition: Snoop.hh:134
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:67
C++ representation of a log entry.
Definition: Snoop.hh:119
pid_t pid
Definition: Snoop.hh:107
std::string loc_method
Definition: Snoop.hh:137
std::string app_name
Definition: Snoop.hh:110
int loc_line
Definition: Snoop.hh:136