20 #include "nlohmann/json.hpp"
24 #include <websocketpp/config/asio_client.hpp>
26 #include <websocketpp/config/asio_no_tls_client.hpp>
28 #include <websocketpp/client.hpp>
29 #include <websocketpp/common/thread.hpp>
30 #include <websocketpp/common/functional.hpp>
37 typedef websocketpp::client<websocketpp::config::asio_tls_client>
WebsocketClient;
41 typedef websocketpp::config::asio_client::message_type::ptr
message_ptr;
43 std::string
safestring(
const unsigned char *c);
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=
"");
77 Snoop&
operator()(
const std::string& type=
"", std::string fl=
"",
int loc=-1, std::string method=
"");
135 bool authenticate(std::function<
void (std::string,
bool)>, std::string user=
"", std::string pass=
"");
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_,
160 std::string
to_json(
bool human_readable)
const;
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&,
188 std::string
to_json(
bool human_readable)
const;
212 PayLoad(
const std::vector<char>& data);
214 std::string
to_json(
bool human_readable)
const;
289 int store_ticket(std::string ticket_uuid,
int user_id,
bool valid);
346 #define LOC __FILE__, __LINE__, __func__
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
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
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