Cadabra
Computer algebra system for field theory problems
|
Logging class with functionality to send log information to a remote server using a websocket connection.
#include <Snoop.hh>
Classes | |
class | AppEntry |
C++ representation of a run entry. More... | |
class | LogEntry |
C++ representation of a log entry. More... | |
class | PayLoad |
C++ representation of a payload entry. More... | |
class | Ticket |
Get status of a given authentication ticket. More... | |
Public Member Functions | |
Snoop () | |
~Snoop () | |
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. More... | |
std::string | get_user_uuid (const std::string &app_name) |
Get a string which uniquely identifies the current user. More... | |
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 file, line number and method. More... | |
void | set_local_type (const std::string &type) |
Determine the 'type' field of records which should not be sent to the remote logging server. More... | |
template<class T > | |
Snoop & | operator<< (const T &obj) |
Generic operator to log an object to the log message being constructed. More... | |
Snoop & | payload (const std::vector< char > &) |
Log payload data. More... | |
Snoop & | operator<< (const Flush &) |
Flush the log entry to disk/server. More... | |
void | set_sync_immediately (bool) |
Set to sync with server after every log line. More... | |
void | sync_with_server (bool from_wsthread=false) |
Ensure that the local database is synchronised with the server (this sends multiple app or log entries in one websocket message). More... | |
void | sync_runs_with_server (bool from_wsthread=false) |
As above, but only for run entries. More... | |
void | sync_logs_with_server (bool from_wsthread=false) |
As above, but only for log entries. More... | |
void | sync_payloads_with_server (bool from_wsthread=false) |
As above, but only for payload data. More... | |
bool | is_connected () const |
Are we connected to the log server? More... | |
std::string | last_seen_version (std::string machine_id) |
Return version of last run seen on given device. More... | |
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 when the response comes back. More... | |
Ticket | is_ticket_valid (std::string ticket_uuid) |
std::string | get_local_ticket () |
Client-side fetching of ticket. More... | |
Protected Member Functions | |
void | start_websocket_client () |
Start the websocket client. More... | |
void | create_tables () |
Ensure that the required tables are present in the database file. More... | |
void | create_authentication_tables () |
Ensure that the required authentication tables are present in the authentication database. More... | |
void | obtain_uuid () |
Obtain a uuid by finding the last AppEntry stored in the local database. More... | |
bool | store_app_entry (Snoop::AppEntry &) |
Store an app entry in the database. More... | |
bool | store_app_entry_without_lock (Snoop::AppEntry &) |
bool | store_log_entry (Snoop::LogEntry &, bool avoid_server_duplicates) |
Store a log entry in the local database. More... | |
bool | store_payload_entry (Snoop::PayLoad &) |
Store payload data in the local database. More... | |
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. More... | |
std::vector< Snoop::AppEntry > | get_app_registrations (std::string uuid_filter="") |
Return a vector of all aps registered in the database. More... | |
int | store_ticket (std::string ticket_uuid, int user_id, bool valid) |
Store an authentication ticket in the database. More... | |
void | set_local_ticket (std::string ticket_uuid) |
Client-side storing of ticket (simpler than store_ticket above). More... | |
Protected Attributes | |
bool | sync_immediately_ |
Variables. More... | |
sqlite3 * | db |
sqlite3 * | payload_db |
sqlite3 * | auth_db |
sqlite3_stmt * | insert_statement |
sqlite3_stmt * | id_for_uuid_statement |
sqlite3_stmt * | payload_insert_statement |
std::recursive_mutex | sqlite_mutex |
Private Member Functions | |
void | on_client_open (websocketpp::connection_hdl hdl) |
void | on_client_fail (websocketpp::connection_hdl hdl) |
void | on_client_close (websocketpp::connection_hdl hdl) |
void | on_client_message (websocketpp::connection_hdl hdl, message_ptr msg) |
Private Attributes | |
WebsocketClient | wsclient |
Websocket client to talk to a remote logging server. More... | |
std::thread | wsclient_thread |
std::mutex | connection_mutex |
std::condition_variable | connection_cv |
bool | connection_is_open |
bool | connection_attempt_failed |
WebsocketClient::connection_ptr | connection |
websocketpp::connection_hdl | our_connection_hdl |
std::ostringstream | out_ |
Snoop::AppEntry | this_app_ |
Snoop::LogEntry | this_log_ |
std::string | server_ |
std::recursive_mutex | call_mutex |
bool | secure |
std::set< std::string > | local_types |
std::function< void(std::string, bool)> | authentication_callback |
Snoop::Snoop | ( | ) |
Snoop::~Snoop | ( | ) |
bool Snoop::authenticate | ( | std::function< void(std::string, bool)> | f, |
std::string | user = "" , |
||
std::string | pass = "" |
||
) |
Authentication logic; passes ticket or credentials to server, and registers callback function for when the response comes back.
|
protected |
Ensure that the required authentication tables are present in the authentication database.
Only used on the server.
|
protected |
Ensure that the required tables are present in the database file.
|
protected |
Return a vector of all aps registered in the database.
If the uuid filter is non-empty, will filter on the given uuid.
std::string Snoop::get_local_ticket | ( | ) |
Client-side fetching of ticket.
std::string Snoop::get_user_uuid | ( | const std::string & | app_name | ) |
Get a string which uniquely identifies the current user.
This is stored in ~/.config/snoop/appname.conf, and in the 'user_id' field in each LogEntry. Note that this is different from the 'uuid' field, which will change from one run to the next.
void Snoop::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.
Should be called once at program startup, but can be called multiple times without causing problems.
bool Snoop::is_connected | ( | ) | const |
Are we connected to the log server?
Snoop::Ticket Snoop::is_ticket_valid | ( | std::string | ticket_uuid | ) |
std::string Snoop::last_seen_version | ( | std::string | machine_id | ) |
Return version of last run seen on given device.
Get the app_version string for the last run on the given device.
|
protected |
Obtain a uuid by finding the last AppEntry stored in the local database.
Will attempt to re-turn a previously generated uuid but will do so only if one is stored for the current pid; if no entry with the current pid is stored then a new one will always be generated.
|
private |
|
private |
|
private |
|
private |
Snoop & 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 file, line number and method.
|
inline |
Generic operator to log an object to the log message being constructed.
Snoop & Snoop::payload | ( | const std::vector< char > & | data | ) |
Log payload data.
|
protected |
Client-side storing of ticket (simpler than store_ticket above).
If ticket is empty, only deletes current ticket.
void Snoop::set_local_type | ( | const std::string & | type | ) |
Determine the 'type' field of records which should not be sent to the remote logging server.
Can be called multiple times.
void Snoop::set_sync_immediately | ( | bool | s | ) |
Set to sync with server after every log line.
|
protected |
Start the websocket client.
This tries to connect to the server and then waits in a separate thread until the server sends us something (typically in response to something the main thread makes by calling wsclient.send).
|
protected |
Store an app entry in the database.
Will update the 'id' field in the AppEntry.
|
protected |
|
protected |
Store an attempt to login into the authentication database.
|
protected |
Store a log entry in the local database.
Generates its own receive_millis field (the one given gets overwritten). Will update the 'id' field in the LogEntry. Returns 'true' if the entry was stored, or 'false' if an entry with this client_log_id was already present (except when it is 0).
|
protected |
Store payload data in the local database.
|
protected |
Store an authentication ticket in the database.
void Snoop::sync_logs_with_server | ( | bool | from_wsthread = false | ) |
As above, but only for log entries.
void Snoop::sync_payloads_with_server | ( | bool | from_wsthread = false | ) |
As above, but only for payload data.
void Snoop::sync_runs_with_server | ( | bool | from_wsthread = false | ) |
As above, but only for run entries.
void Snoop::sync_with_server | ( | bool | from_wsthread = false | ) |
Ensure that the local database is synchronised with the server (this sends multiple app or log entries in one websocket message).
Leave the bool argument at its default argument under all normal circumstances.
|
protected |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
protected |
|
protected |
|
protected |
|
private |
|
private |
|
private |
|
protected |
|
protected |
|
private |
|
private |
|
protected |
|
protected |
Variables.
|
private |
|
private |
|
private |
Websocket client to talk to a remote logging server.
|
private |