Cadabra
Computer algebra system for field theory problems
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
snoop::Snoop Class Reference

Description

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...
 
Snoopoperator() (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 >
Snoopoperator<< (const T &obj)
 Generic operator to log an object to the log message being constructed. More...
 
Snooppayload (const std::vector< char > &)
 Log payload data. More...
 
Snoopoperator<< (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::AppEntryget_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
 

Constructor & Destructor Documentation

◆ Snoop()

Snoop::Snoop ( )

◆ ~Snoop()

Snoop::~Snoop ( )

Member Function Documentation

◆ authenticate()

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.

◆ create_authentication_tables()

void Snoop::create_authentication_tables ( )
protected

Ensure that the required authentication tables are present in the authentication database.

Only used on the server.

◆ create_tables()

void Snoop::create_tables ( )
protected

Ensure that the required tables are present in the database file.

◆ get_app_registrations()

std::vector< Snoop::AppEntry > Snoop::get_app_registrations ( std::string  uuid_filter = "")
protected

Return a vector of all aps registered in the database.

If the uuid filter is non-empty, will filter on the given uuid.

◆ get_local_ticket()

std::string Snoop::get_local_ticket ( )

Client-side fetching of ticket.

◆ get_user_uuid()

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.

◆ init()

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.

◆ is_connected()

bool Snoop::is_connected ( ) const

Are we connected to the log server?

◆ is_ticket_valid()

Snoop::Ticket Snoop::is_ticket_valid ( std::string  ticket_uuid)

◆ last_seen_version()

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.

◆ obtain_uuid()

void Snoop::obtain_uuid ( )
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.

◆ on_client_close()

void Snoop::on_client_close ( websocketpp::connection_hdl  hdl)
private

◆ on_client_fail()

void Snoop::on_client_fail ( websocketpp::connection_hdl  hdl)
private

◆ on_client_message()

void Snoop::on_client_message ( websocketpp::connection_hdl  hdl,
message_ptr  msg 
)
private

◆ on_client_open()

void Snoop::on_client_open ( websocketpp::connection_hdl  hdl)
private

◆ operator()()

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.

◆ operator<<() [1/2]

Snoop & Snoop::operator<< ( const Flush )

Flush the log entry to disk/server.

◆ operator<<() [2/2]

template<class T >
Snoop& snoop::Snoop::operator<< ( const T &  obj)
inline

Generic operator to log an object to the log message being constructed.

◆ payload()

Snoop & Snoop::payload ( const std::vector< char > &  data)

Log payload data.

◆ set_local_ticket()

void Snoop::set_local_ticket ( std::string  ticket_uuid)
protected

Client-side storing of ticket (simpler than store_ticket above).

If ticket is empty, only deletes current ticket.

◆ set_local_type()

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.

◆ set_sync_immediately()

void Snoop::set_sync_immediately ( bool  s)

Set to sync with server after every log line.

◆ start_websocket_client()

void Snoop::start_websocket_client ( )
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).

◆ store_app_entry()

bool Snoop::store_app_entry ( Snoop::AppEntry app)
protected

Store an app entry in the database.

Will update the 'id' field in the AppEntry.

◆ store_app_entry_without_lock()

bool Snoop::store_app_entry_without_lock ( Snoop::AppEntry app)
protected

◆ store_auth_attempt_entry()

bool Snoop::store_auth_attempt_entry ( int  user_id,
int  ticket_id,
int  valid,
std::string  msg 
)
protected

Store an attempt to login into the authentication database.

◆ store_log_entry()

bool Snoop::store_log_entry ( Snoop::LogEntry log_entry,
bool  avoid_server_duplicates 
)
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).

◆ store_payload_entry()

bool Snoop::store_payload_entry ( Snoop::PayLoad payload)
protected

Store payload data in the local database.

◆ store_ticket()

int Snoop::store_ticket ( std::string  ticket_uuid,
int  user_id,
bool  valid 
)
protected

Store an authentication ticket in the database.

◆ sync_logs_with_server()

void Snoop::sync_logs_with_server ( bool  from_wsthread = false)

As above, but only for log entries.

◆ sync_payloads_with_server()

void Snoop::sync_payloads_with_server ( bool  from_wsthread = false)

As above, but only for payload data.

◆ sync_runs_with_server()

void Snoop::sync_runs_with_server ( bool  from_wsthread = false)

As above, but only for run entries.

◆ sync_with_server()

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.

Member Data Documentation

◆ auth_db

sqlite3 * snoop::Snoop::auth_db
protected

◆ authentication_callback

std::function<void (std::string, bool)> snoop::Snoop::authentication_callback
private

◆ call_mutex

std::recursive_mutex snoop::Snoop::call_mutex
private

◆ connection

WebsocketClient::connection_ptr snoop::Snoop::connection
private

◆ connection_attempt_failed

bool snoop::Snoop::connection_attempt_failed
private

◆ connection_cv

std::condition_variable snoop::Snoop::connection_cv
private

◆ connection_is_open

bool snoop::Snoop::connection_is_open
private

◆ connection_mutex

std::mutex snoop::Snoop::connection_mutex
private

◆ db

sqlite3* snoop::Snoop::db
protected

◆ id_for_uuid_statement

sqlite3_stmt * snoop::Snoop::id_for_uuid_statement
protected

◆ insert_statement

sqlite3_stmt* snoop::Snoop::insert_statement
protected

◆ local_types

std::set<std::string> snoop::Snoop::local_types
private

◆ our_connection_hdl

websocketpp::connection_hdl snoop::Snoop::our_connection_hdl
private

◆ out_

std::ostringstream snoop::Snoop::out_
private

◆ payload_db

sqlite3 * snoop::Snoop::payload_db
protected

◆ payload_insert_statement

sqlite3_stmt * snoop::Snoop::payload_insert_statement
protected

◆ secure

bool snoop::Snoop::secure
private

◆ server_

std::string snoop::Snoop::server_
private

◆ sqlite_mutex

std::recursive_mutex snoop::Snoop::sqlite_mutex
protected

◆ sync_immediately_

bool snoop::Snoop::sync_immediately_
protected

Variables.

◆ this_app_

Snoop::AppEntry snoop::Snoop::this_app_
private

◆ this_log_

Snoop::LogEntry snoop::Snoop::this_log_
private

◆ wsclient

WebsocketClient snoop::Snoop::wsclient
private

Websocket client to talk to a remote logging server.

◆ wsclient_thread

std::thread snoop::Snoop::wsclient_thread
private

The documentation for this class was generated from the following files: