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-2020 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 "nlohmann/json.hpp"
21 #include <thread>
22 #include <set>
23 #ifdef SNOOP_SSL
24  #include <websocketpp/config/asio_client.hpp>
25 #else
26  #include <websocketpp/config/asio_no_tls_client.hpp>
27 #endif
28 #include <websocketpp/client.hpp>
29 #include <websocketpp/common/thread.hpp>
30 #include <websocketpp/common/functional.hpp>
31 
32 #ifndef _MSC_VER
33  #include <unistd.h>
34 #endif
35 
36 #ifdef SNOOP_SSL
37  typedef websocketpp::client<websocketpp::config::asio_tls_client> WebsocketClient;
38 #else
39  typedef websocketpp::client<websocketpp::config::asio_client> WebsocketClient;
40 #endif
41 typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
42 
43 std::string safestring(const unsigned char *c);
44 
45 namespace snoop {
46 
47  class SnoopImpl;
48  class Flush {};
49  extern Flush flush;
50 
53 
54  class Snoop {
55  public:
56  Snoop();
57  ~Snoop();
58 
62 
63  void init(const std::string& app_name, const std::string& app_version,
64  std::string server="", std::string local_log_file="", std::string machine_id="");
65 
70 
71  std::string get_user_uuid(const std::string& app_name);
72 
76 
77  Snoop& operator()(const std::string& type="", std::string fl="", int loc=-1, std::string method="");
78 
81 
82  void set_local_type(const std::string& type);
83 
85 
86  template<class T>
87  Snoop& operator<<(const T& obj) {
88  out_ <<(obj);
89  return *this;
90  }
91 
93 
94  Snoop& payload(const std::vector<char>&);
95 
97 
98  Snoop& operator<<(const Flush&);
99 
101 
102  void set_sync_immediately(bool);
103 
108 
109  void sync_with_server(bool from_wsthread=false);
110 
112 
113  void sync_runs_with_server(bool from_wsthread=false);
114 
116 
117  void sync_logs_with_server(bool from_wsthread=false);
118 
120 
121  void sync_payloads_with_server(bool from_wsthread=false);
122 
124 
125  bool is_connected() const;
126 
128 
129  std::string last_seen_version(std::string machine_id);
130 
134 
135  bool authenticate(std::function<void (std::string, bool)>, std::string user="", std::string pass="");
136 
138 
139  class Ticket {
140  public:
141  Ticket();
143  int user_id;
144  std::string ticket_uuid;
145  bool valid;
146  };
147  Ticket is_ticket_valid(std::string ticket_uuid);
148 
150 
151  class AppEntry {
152  public:
153  AppEntry();
154  AppEntry(const std::string& uuid_, uint64_t create_millis_, uint64_t receive_millis_, uint64_t pid_,
155  const std::string& ip_address_, const std::string& machine_id_,
156  const std::string& app_name_, const std::string& app_version_,
157  const std::string& user_id_,
158  int server_status_, int create_timezone);
159 
160  std::string to_json(bool human_readable) const;
161  void from_json(const nlohmann::json&);
162 
163  int id;
164  std::string uuid;
165  uint64_t create_millis;
166  uint64_t receive_millis;
167  uint64_t pid;
168  std::string ip_address;
169  std::string machine_id;
170  std::string app_name;
171  std::string app_version;
172  std::string user_id;
173  int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
174  bool connected;
176  };
177 
179 
180  class LogEntry {
181  public:
182  LogEntry();
183  LogEntry(int log_id_, int client_log_id_, int id_, const std::string&,
184  uint64_t, uint64_t, const std::string&, int, const std::string&,
185  const std::string& , const std::string&, int status, const std::string&,
186  int create_timezone);
187 
188  std::string to_json(bool human_readable) const;
189  void from_json(const nlohmann::json&);
190 
191  int log_id;
193  int id;
194  std::string uuid; // this goes on the wire, but is not stored on disk.
195  uint64_t create_millis;
196  uint64_t receive_millis;
197  std::string loc_file;
198  int loc_line;
199  std::string loc_method;
200  std::string type;
201  std::string message;
202  int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
203  std::string session_uuid;
205  };
206 
208 
209  class PayLoad {
210  public:
211  PayLoad();
212  PayLoad(const std::vector<char>& data);
213 
214  std::string to_json(bool human_readable) const;
215  void from_json(const nlohmann::json&);
216 
219  int id;
220  std::string uuid; // this goes on the wire, but is not stored on disk.
221  uint64_t create_millis;
222  uint64_t receive_millis;
223  std::string payload;
224  int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
226  };
227 
229 
230  std::string get_local_ticket();
231 
232 
233  protected:
237 
238  void start_websocket_client();
239 
242 
243  void create_tables();
244 
247 
249 
255 
256  void obtain_uuid();
257 
260 
263 
270 
271  bool store_log_entry(Snoop::LogEntry&, bool avoid_server_duplicates);
272 
274 
276 
278 
279  bool store_auth_attempt_entry(int user_id, int ticket_id, int valid, std::string msg);
280 
284 
285  std::vector<Snoop::AppEntry> get_app_registrations(std::string uuid_filter="");
286 
288 
289  int store_ticket(std::string ticket_uuid, int user_id, bool valid);
290 
293 
294  void set_local_ticket(std::string ticket_uuid);
295 
297 
299  sqlite3 *db, *payload_db, *auth_db;
301  std::recursive_mutex sqlite_mutex;
302 
303  private:
305 
307  std::thread wsclient_thread;
308  std::mutex connection_mutex;
309  std::condition_variable connection_cv;
311  WebsocketClient::connection_ptr connection;
312  websocketpp::connection_hdl our_connection_hdl;
313 
314  void on_client_open(websocketpp::connection_hdl hdl);
315  void on_client_fail(websocketpp::connection_hdl hdl);
316  void on_client_close(websocketpp::connection_hdl hdl);
317  void on_client_message(websocketpp::connection_hdl hdl, message_ptr msg);
318 
319  std::ostringstream out_;
320 
323  std::string server_;
324 
325  std::recursive_mutex call_mutex;
326  bool secure;
327 
328  std::set<std::string> local_types;
329 // std::set<std::string> ...
330  std::function<void (std::string, bool)> authentication_callback;
331 
332  };
333 
334  extern Snoop log;
335 
336  const char info[] ="info";
337  const char warn[] ="warning";
338  const char error[]="error";
339  const char fatal[]="fatal";
340  const char email[]="email";
341 }
342 
343 // set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst
344 // ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
345 
346 #define LOC __FILE__, __LINE__, __func__
347 
websocketpp::config::asio_client::message_type::ptr message_ptr
Definition: ComputeThread.hh:14
websocketpp::client< websocketpp::config::asio_client > WebsocketClient
Definition: Snoop.hh:39
std::string safestring(const unsigned char *c)
Definition: Snoop.cc:89
websocketpp::config::asio_client::message_type::ptr message_ptr
Definition: Snoop.hh:41
Definition: Snoop.hh:48
C++ representation of a run entry.
Definition: Snoop.hh:151
std::string to_json(bool human_readable) const
Definition: Snoop.cc:1610
int create_timezone
Definition: Snoop.hh:175
std::string ip_address
Definition: Snoop.hh:168
int server_status
Definition: Snoop.hh:173
uint64_t create_millis
Definition: Snoop.hh:165
uint64_t pid
Definition: Snoop.hh:167
AppEntry()
Definition: Snoop.cc:1594
std::string user_id
Definition: Snoop.hh:172
std::string machine_id
Definition: Snoop.hh:169
int id
Definition: Snoop.hh:163
void from_json(const nlohmann::json &)
Definition: Snoop.cc:1698
uint64_t receive_millis
Definition: Snoop.hh:166
std::string app_version
Definition: Snoop.hh:171
std::string uuid
Definition: Snoop.hh:164
bool connected
Definition: Snoop.hh:174
std::string app_name
Definition: Snoop.hh:170
C++ representation of a log entry.
Definition: Snoop.hh:180
std::string loc_file
Definition: Snoop.hh:197
int loc_line
Definition: Snoop.hh:198
int client_log_id
Definition: Snoop.hh:192
std::string to_json(bool human_readable) const
Definition: Snoop.cc:1552
int create_timezone
Definition: Snoop.hh:204
int id
Definition: Snoop.hh:193
std::string message
Definition: Snoop.hh:201
std::string session_uuid
Definition: Snoop.hh:203
std::string loc_method
Definition: Snoop.hh:199
void from_json(const nlohmann::json &)
Definition: Snoop.cc:1677
uint64_t receive_millis
Definition: Snoop.hh:196
std::string type
Definition: Snoop.hh:200
LogEntry()
Definition: Snoop.cc:1538
std::string uuid
Definition: Snoop.hh:194
uint64_t create_millis
Definition: Snoop.hh:195
int server_status
Definition: Snoop.hh:202
int log_id
Definition: Snoop.hh:191
C++ representation of a payload entry.
Definition: Snoop.hh:209
int payload_id
Definition: Snoop.hh:217
std::string payload
Definition: Snoop.hh:223
uint64_t receive_millis
Definition: Snoop.hh:222
PayLoad()
Definition: Snoop.cc:1645
std::string to_json(bool human_readable) const
Definition: Snoop.cc:1658
int client_payload_id
Definition: Snoop.hh:218
uint64_t create_millis
Definition: Snoop.hh:221
int server_status
Definition: Snoop.hh:224
int id
Definition: Snoop.hh:219
std::string uuid
Definition: Snoop.hh:220
void from_json(const nlohmann::json &)
Definition: Snoop.cc:1718
int create_timezone
Definition: Snoop.hh:225
Get status of a given authentication ticket.
Definition: Snoop.hh:139
std::string ticket_uuid
Definition: Snoop.hh:144
bool valid
Definition: Snoop.hh:145
Ticket()
Definition: Snoop.cc:635
int user_id
Definition: Snoop.hh:143
int ticket_id
Definition: Snoop.hh:142
Logging class with functionality to send log information to a remote server using a websocket connect...
Definition: Snoop.hh:54
std::thread wsclient_thread
Definition: Snoop.hh:307
Snoop & operator<<(const T &obj)
Generic operator to log an object to the log message being constructed.
Definition: Snoop.hh:87
std::string server_
Definition: Snoop.hh:323
sqlite3 * payload_db
Definition: Snoop.hh:299
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:1471
std::condition_variable connection_cv
Definition: Snoop.hh:309
std::recursive_mutex call_mutex
Definition: Snoop.hh:325
int store_ticket(std::string ticket_uuid, int user_id, bool valid)
Store an authentication ticket in the database.
Definition: Snoop.cc:677
std::set< std::string > local_types
Definition: Snoop.hh:328
void on_client_message(websocketpp::connection_hdl hdl, message_ptr msg)
Definition: Snoop.cc:1311
void on_client_fail(websocketpp::connection_hdl hdl)
Definition: Snoop.cc:1289
WebsocketClient wsclient
Websocket client to talk to a remote logging server.
Definition: Snoop.hh:306
void set_local_type(const std::string &type)
Determine the 'type' field of records which should not be sent to the remote logging server.
Definition: Snoop.cc:1466
void on_client_close(websocketpp::connection_hdl hdl)
Definition: Snoop.cc:1300
sqlite3 * db
Definition: Snoop.hh:299
std::mutex connection_mutex
Definition: Snoop.hh:308
WebsocketClient::connection_ptr connection
Definition: Snoop.hh:311
bool connection_is_open
Definition: Snoop.hh:310
sqlite3_stmt * insert_statement
Definition: Snoop.hh:300
void obtain_uuid()
Obtain a uuid by finding the last AppEntry stored in the local database.
Definition: Snoop.cc:480
std::string get_user_uuid(const std::string &app_name)
Get a string which uniquely identifies the current user.
Definition: Snoop.cc:228
bool secure
Definition: Snoop.hh:326
std::ostringstream out_
Definition: Snoop.hh:319
Snoop::AppEntry this_app_
Definition: Snoop.hh:321
bool connection_attempt_failed
Definition: Snoop.hh:310
void set_sync_immediately(bool)
Set to sync with server after every log line.
Definition: Snoop.cc:285
void set_local_ticket(std::string ticket_uuid)
Client-side storing of ticket (simpler than store_ticket above).
Definition: Snoop.cc:613
~Snoop()
Definition: Snoop.cc:1423
std::string get_local_ticket()
Client-side fetching of ticket.
Definition: Snoop.cc:590
Snoop & payload(const std::vector< char > &)
Log payload data.
Definition: Snoop.cc:1517
Snoop::LogEntry this_log_
Definition: Snoop.hh:322
bool store_app_entry_without_lock(Snoop::AppEntry &)
Definition: Snoop.cc:708
sqlite3 * auth_db
Definition: Snoop.hh:299
bool store_auth_attempt_entry(int user_id, int ticket_id, int valid, std::string msg)
Store an attempt to login into the authentication database.
Definition: Snoop.cc:819
void on_client_open(websocketpp::connection_hdl hdl)
Definition: Snoop.cc:1277
std::function< void(std::string, bool)> authentication_callback
Definition: Snoop.hh:330
sqlite3_stmt * payload_insert_statement
Definition: Snoop.hh:300
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:955
bool store_payload_entry(Snoop::PayLoad &)
Store payload data in the local database.
Definition: Snoop.cc:848
std::vector< Snoop::AppEntry > get_app_registrations(std::string uuid_filter="")
Return a vector of all aps registered in the database.
Definition: Snoop.cc:1224
void sync_runs_with_server(bool from_wsthread=false)
As above, but only for run entries.
Definition: Snoop.cc:968
Snoop()
Definition: Snoop.cc:95
bool sync_immediately_
Variables.
Definition: Snoop.hh:298
bool store_log_entry(Snoop::LogEntry &, bool avoid_server_duplicates)
Store a log entry in the local database.
Definition: Snoop.cc:756
bool store_app_entry(Snoop::AppEntry &)
Store an app entry in the database.
Definition: Snoop.cc:699
std::string last_seen_version(std::string machine_id)
Return version of last run seen on given device.
Definition: Snoop.cc:516
void sync_logs_with_server(bool from_wsthread=false)
As above, but only for log entries.
Definition: Snoop.cc:1046
websocketpp::connection_hdl our_connection_hdl
Definition: Snoop.hh:312
Ticket is_ticket_valid(std::string ticket_uuid)
Definition: Snoop.cc:642
void start_websocket_client()
Start the websocket client.
Definition: Snoop.cc:903
void init(const std::string &app_name, const std::string &app_version, std::string server="", std::string local_log_file="", std::string machine_id="")
Initialise the logging stream.
Definition: Snoop.cc:105
void sync_payloads_with_server(bool from_wsthread=false)
As above, but only for payload data.
Definition: Snoop.cc:1142
sqlite3_stmt * id_for_uuid_statement
Definition: Snoop.hh:300
std::recursive_mutex sqlite_mutex
Definition: Snoop.hh:301
void create_authentication_tables()
Ensure that the required authentication tables are present in the authentication database.
Definition: Snoop.cc:413
bool authenticate(std::function< void(std::string, bool)>, std::string user="", std::string pass="")
Authentication logic; passes ticket or credentials to server, and registers callback function for whe...
Definition: Snoop.cc:541
bool is_connected() const
Are we connected to the log server?
Definition: Snoop.cc:1137
void create_tables()
Ensure that the required tables are present in the database file.
Definition: Snoop.cc:290
Definition: SnoopPrivate.hh:18
server
Definition: cadabra2_defaults.py:156
Definition: Snoop.hh:45
const char fatal[]
Definition: Snoop.hh:339
const char info[]
Definition: Snoop.hh:336
const char warn[]
Definition: Snoop.hh:337
Flush flush
Definition: Snoop.cc:63
const char email[]
Definition: Snoop.hh:340
Snoop log
Definition: Snoop.cc:62
const char error[]
Definition: Snoop.hh:338