Cadabra
Computer algebra system for field theory problems
Loading...
Searching...
No Matches
Snoop.hh
Go to the documentation of this file.
1
2/*
3
4 Snoop
5 Copyright (C) 2015-2024 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#ifndef __ANDROID__
16 // Android uses Java/Kotlin networking, don't attempt to do it native.
17 #define SNOOP_SSL
18#endif
19
20#include <string>
21#include <sstream>
22#include <sqlite3.h>
23#include <stdint.h>
24#include <mutex>
25#include "nlohmann/json.hpp"
26#include <thread>
27#include <condition_variable>
28#include <set>
29
30#include "websocket_client.hh"
31
32#ifndef _MSC_VER
33 #include <unistd.h>
34#endif
35
36std::string safestring(const unsigned char *c);
37
38namespace snoop {
39
40 class SnoopImpl;
41 class Flush {};
42 extern Flush flush;
43
46
47 class Snoop {
48 public:
49 Snoop();
50 ~Snoop();
51
55
56 void init(const std::string& app_name, const std::string& app_version,
57 std::string server="", std::string local_log_file="", std::string machine_id="");
58
63
64 std::string get_user_uuid(const std::string& app_name);
65
69
70 Snoop& operator()(const std::string& type="", std::string fl="", int loc=-1, std::string method="");
71
74
75 void set_local_type(const std::string& type);
76
78
79 template<class T>
80 Snoop& operator<<(const T& obj) {
81 out_ <<(obj);
82 return *this;
83 }
84
86
87 Snoop& payload(const std::vector<char>&);
88
90
91 Snoop& operator<<(const Flush&);
92
94
95 void set_sync_immediately(bool);
96
101
102 void sync_with_server(bool from_wsthread=false);
103
105
106 void sync_runs_with_server(bool from_wsthread=false);
107
109
110 void sync_logs_with_server(bool from_wsthread=false);
111
113
114 void sync_payloads_with_server(bool from_wsthread=false);
115
117
118 bool is_connected() const;
119
121
122 std::string last_seen_version(std::string machine_id);
123
129
130 bool authenticate(std::function<void (std::string, bool)>, std::string user="", std::string pass="", bool always_reauth=false);
137
138 void set_error_handler(std::function<bool (std::string)>);
139
141
142 class ParseError : public std::logic_error {
143 public:
144 ParseError(const std::string&);
145 };
146
148
149 class Ticket {
150 public:
151 Ticket();
154 std::string ticket_uuid;
155 bool valid;
156 };
157 Ticket is_ticket_valid(std::string ticket_uuid);
158
160
161 class AppEntry {
162 public:
163 AppEntry();
164 AppEntry(const std::string& uuid_, uint64_t create_millis_, uint64_t receive_millis_, uint64_t pid_,
165 const std::string& ip_address_, const std::string& machine_id_,
166 const std::string& app_name_, const std::string& app_version_,
167 const std::string& user_id_,
168 int server_status_, int create_timezone);
169
170 std::string to_json(bool human_readable) const;
171 void from_json(const nlohmann::json&);
172
173 int id;
174 std::string uuid;
177 uint64_t pid;
178 std::string ip_address;
179 std::string machine_id;
180 std::string app_name;
181 std::string app_version;
182 std::string user_id;
183 int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
186 };
187
189
190 class LogEntry {
191 public:
192 LogEntry();
193 LogEntry(int log_id_, int client_log_id_, int id_, const std::string&,
194 uint64_t, uint64_t, const std::string&, int, const std::string&,
195 const std::string& , const std::string&, int status, const std::string&,
196 int create_timezone);
197
198 std::string to_json(bool human_readable) const;
199 void from_json(const nlohmann::json&);
200
203 int id;
204 std::string uuid; // this goes on the wire, but is not stored on disk.
207 std::string loc_file;
209 std::string loc_method;
210 std::string type;
211 std::string message;
212 int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
213 std::string session_uuid;
215 };
216
218
219 class PayLoad {
220 public:
221 PayLoad();
222 PayLoad(const std::vector<char>& data);
223
224 std::string to_json(bool human_readable) const;
225 void from_json(const nlohmann::json&);
226
229 int id;
230 std::string uuid; // this goes on the wire, but is not stored on disk.
233 std::string payload;
234 int server_status; // 1: synced, 0 and negative: number of attempts at syncing made
236 };
237
239
240 std::string get_local_ticket();
241
244
245 std::string local_user() const;
246
248
249 void set_session_uuid(const std::string&);
250
255
256 bool add_user(std::string user, std::string password, bool single=false);
257
258 protected:
262
264
267
268 void create_tables();
269
272
274
280
281 void obtain_uuid();
282
285
288
295
296 bool store_log_entry(Snoop::LogEntry&, bool avoid_server_duplicates);
297
299
301
303
304 bool store_auth_attempt_entry(int user_id, int ticket_id, int valid, std::string msg);
305
309
310 std::vector<Snoop::AppEntry> get_app_registrations(std::string uuid_filter="");
311
313
314 int store_ticket(std::string ticket_uuid, int user_id, bool valid);
315
318
319 void set_local_ticket(std::string ticket_uuid);
320
322
324 sqlite3 *db, *payload_db, *auth_db;
326 std::recursive_mutex sqlite_mutex;
327
328 private:
330
332 std::thread wsclient_thread;
334 std::condition_variable connection_cv;
336
337 // Main entry point for the I/O thread.
338 void io_thread_run();
339 void try_connect();
340
341 void on_client_open();
342 void on_client_fail(const boost::beast::error_code& ec);
343 void on_client_close();
344 void on_client_message(const std::string& msg);
345
346 std::ostringstream out_;
347
350 std::string server_;
351 std::string session_uuid_; // gets copied into every LogEntry.
352
353 std::recursive_mutex call_mutex;
354
355 std::set<std::string> local_types;
356 std::function<void (std::string, bool)> authentication_callback;
357 std::function<bool (std::string)> error_callback;
358
359 };
360
361 extern Snoop log;
362
363 const char info[] ="info";
364 const char warn[] ="warning";
365 const char error[]="error";
366 const char fatal[]="fatal";
367 const char email[]="email";
368}
369
370// set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst
371// ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
372
373#define LOC __FILE__, __LINE__, __func__
374
std::string safestring(const unsigned char *c)
Definition Snoop.cc:93
Definition Snoop.hh:41
C++ representation of a run entry.
Definition Snoop.hh:161
std::string to_json(bool human_readable) const
Definition Snoop.cc:1723
int create_timezone
Definition Snoop.hh:185
std::string ip_address
Definition Snoop.hh:178
int server_status
Definition Snoop.hh:183
uint64_t create_millis
Definition Snoop.hh:175
uint64_t pid
Definition Snoop.hh:177
AppEntry()
Definition Snoop.cc:1707
std::string user_id
Definition Snoop.hh:182
std::string machine_id
Definition Snoop.hh:179
int id
Definition Snoop.hh:173
void from_json(const nlohmann::json &)
Definition Snoop.cc:1824
uint64_t receive_millis
Definition Snoop.hh:176
std::string app_version
Definition Snoop.hh:181
std::string uuid
Definition Snoop.hh:174
bool connected
Definition Snoop.hh:184
std::string app_name
Definition Snoop.hh:180
C++ representation of a log entry.
Definition Snoop.hh:190
std::string loc_file
Definition Snoop.hh:207
int loc_line
Definition Snoop.hh:208
int client_log_id
Definition Snoop.hh:202
std::string to_json(bool human_readable) const
Definition Snoop.cc:1665
int create_timezone
Definition Snoop.hh:214
int id
Definition Snoop.hh:203
std::string message
Definition Snoop.hh:211
std::string session_uuid
Definition Snoop.hh:213
std::string loc_method
Definition Snoop.hh:209
void from_json(const nlohmann::json &)
Definition Snoop.cc:1790
uint64_t receive_millis
Definition Snoop.hh:206
std::string type
Definition Snoop.hh:210
LogEntry()
Definition Snoop.cc:1651
std::string uuid
Definition Snoop.hh:204
uint64_t create_millis
Definition Snoop.hh:205
int server_status
Definition Snoop.hh:212
int log_id
Definition Snoop.hh:201
Exception used to flag invalid/unparseable data received on the wire.
Definition Snoop.hh:142
C++ representation of a payload entry.
Definition Snoop.hh:219
int payload_id
Definition Snoop.hh:227
std::string payload
Definition Snoop.hh:233
uint64_t receive_millis
Definition Snoop.hh:232
PayLoad()
Definition Snoop.cc:1758
std::string to_json(bool human_readable) const
Definition Snoop.cc:1771
int client_payload_id
Definition Snoop.hh:228
uint64_t create_millis
Definition Snoop.hh:231
int server_status
Definition Snoop.hh:234
int id
Definition Snoop.hh:229
std::string uuid
Definition Snoop.hh:230
void from_json(const nlohmann::json &)
Definition Snoop.cc:1854
int create_timezone
Definition Snoop.hh:235
Get status of a given authentication ticket.
Definition Snoop.hh:149
std::string ticket_uuid
Definition Snoop.hh:154
bool valid
Definition Snoop.hh:155
Ticket()
Definition Snoop.cc:710
int user_id
Definition Snoop.hh:153
int ticket_id
Definition Snoop.hh:152
Logging class with functionality to send log information to a remote server using a websocket connect...
Definition Snoop.hh:47
std::thread wsclient_thread
Definition Snoop.hh:332
std::string server_
Definition Snoop.hh:350
sqlite3 * payload_db
Definition Snoop.hh:324
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:1583
std::condition_variable connection_cv
Definition Snoop.hh:334
std::recursive_mutex call_mutex
Definition Snoop.hh:353
void on_client_open()
Definition Snoop.cc:1363
int store_ticket(std::string ticket_uuid, int user_id, bool valid)
Store an authentication ticket in the database.
Definition Snoop.cc:752
std::set< std::string > local_types
Definition Snoop.hh:355
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:1578
sqlite3 * db
Definition Snoop.hh:324
std::mutex connection_mutex
Definition Snoop.hh:333
bool connection_is_open
Definition Snoop.hh:335
sqlite3_stmt * insert_statement
Definition Snoop.hh:325
void on_client_fail(const boost::beast::error_code &ec)
Definition Snoop.cc:1374
std::string local_user() const
Retrieve the (single) local username from the database, empty string if no such row exists.
Definition Snoop.cc:1909
bool add_user(std::string user, std::string password, bool single=false)
Add a user/password combo to the user database.
Definition Snoop.cc:1869
void obtain_uuid()
Obtain a uuid by finding the last AppEntry stored in the local database.
Definition Snoop.cc:518
std::string get_user_uuid(const std::string &app_name)
Get a string which uniquely identifies the current user.
Definition Snoop.cc:253
void io_thread_run()
Definition Snoop.cc:1021
void set_error_handler(std::function< bool(std::string)>)
Set error handler for clients; will be called on any networking errors.
Definition Snoop.cc:655
std::ostringstream out_
Definition Snoop.hh:346
Snoop::AppEntry this_app_
Definition Snoop.hh:348
bool connection_attempt_failed
Definition Snoop.hh:335
std::string session_uuid_
Definition Snoop.hh:351
void set_sync_immediately(bool)
Set to sync with server after every log line.
Definition Snoop.cc:310
void set_local_ticket(std::string ticket_uuid)
Client-side storing of ticket (simpler than store_ticket above).
Definition Snoop.cc:688
~Snoop()
Definition Snoop.cc:1535
std::string get_local_ticket()
Client-side fetching of ticket.
Definition Snoop.cc:665
Snoop & payload(const std::vector< char > &)
Log payload data.
Definition Snoop.cc:1630
sqlite3_stmt * testq_statement
Definition Snoop.hh:325
Snoop::LogEntry this_log_
Definition Snoop.hh:349
bool store_app_entry_without_lock(Snoop::AppEntry &)
Definition Snoop.cc:783
sqlite3 * auth_db
Definition Snoop.hh:324
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:899
std::function< void(std::string, bool)> authentication_callback
Definition Snoop.hh:356
sqlite3_stmt * payload_insert_statement
Definition Snoop.hh:325
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:1033
bool store_payload_entry(Snoop::PayLoad &)
Store payload data in the local database.
Definition Snoop.cc:928
std::vector< Snoop::AppEntry > get_app_registrations(std::string uuid_filter="")
Return a vector of all aps registered in the database.
Definition Snoop.cc:1303
void sync_runs_with_server(bool from_wsthread=false)
As above, but only for run entries.
Definition Snoop.cc:1046
void try_connect()
Definition Snoop.cc:1002
std::function< bool(std::string)> error_callback
Definition Snoop.hh:357
Snoop()
Definition Snoop.cc:99
bool sync_immediately_
Variables.
Definition Snoop.hh:323
bool store_log_entry(Snoop::LogEntry &, bool avoid_server_duplicates)
Store a log entry in the local database.
Definition Snoop.cc:831
bool authenticate(std::function< void(std::string, bool)>, std::string user="", std::string pass="", bool always_reauth=false)
Authentication logic; passes ticket or credentials to server, and registers callback function for whe...
Definition Snoop.cc:589
bool store_app_entry(Snoop::AppEntry &)
Store an app entry in the database.
Definition Snoop.cc:774
std::string last_seen_version(std::string machine_id)
Return version of last run seen on given device.
Definition Snoop.cc:564
void sync_logs_with_server(bool from_wsthread=false)
As above, but only for log entries.
Definition Snoop.cc:1125
void set_session_uuid(const std::string &)
Set the session uuid so log entries can be easily grouped by session.
Definition Snoop.cc:660
Snoop & operator<<(const T &obj)
Generic operator to log an object to the log message being constructed.
Definition Snoop.hh:80
Ticket is_ticket_valid(std::string ticket_uuid)
Definition Snoop.cc:717
void start_websocket_client()
Start the websocket client.
Definition Snoop.cc:983
void on_client_message(const std::string &msg)
Definition Snoop.cc:1422
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:118
void sync_payloads_with_server(bool from_wsthread=false)
As above, but only for payload data.
Definition Snoop.cc:1221
sqlite3_stmt * id_for_uuid_statement
Definition Snoop.hh:325
void on_client_close()
Definition Snoop.cc:1403
std::recursive_mutex sqlite_mutex
Definition Snoop.hh:326
void create_authentication_tables()
Ensure that the required authentication tables are present in the authentication database.
Definition Snoop.cc:443
websocket_client wsclient
Websocket client to talk to a remote logging server.
Definition Snoop.hh:331
bool is_connected() const
Are we connected to the log server?
Definition Snoop.cc:1216
void create_tables()
Ensure that the required tables are present in the database file.
Definition Snoop.cc:315
Definition websocket_client.hh:15
Definition Snoop.hh:38
const char fatal[]
Definition Snoop.hh:366
const char info[]
Definition Snoop.hh:363
const char warn[]
Definition Snoop.hh:364
Flush flush
Definition Snoop.cc:71
const char email[]
Definition Snoop.hh:367
Snoop log
Definition Snoop.cc:70
const char error[]
Definition Snoop.hh:365