28 #include <unordered_map> 29 #include "easylogging++.h" 31 class ResidueDispatcher;
49 void residueCrashHandler(
int) noexcept;
62 ALLOW_UNKNOWN_LOGGERS = 1,
63 ALLOW_BULK_LOG_REQUEST = 16,
67 enum class InternalLoggingLevel : std::int8_t {
76 #ifdef RESIDUE_PROFILING 77 unsigned long queue_ms;
78 unsigned long dispatch_ms;
128 static std::
string version() noexcept;
133 static std::
string info() noexcept;
141 static inline
void connect(const std::
string& host,
int port)
161 static inline void connect(
const std::string& host)
183 static inline void reconnect(
const std::string& host =
"",
int port = -1)
202 static inline const std::string&
clientId() noexcept
243 static inline
void wait() noexcept
274 throw std::invalid_argument(
"Bulk log requests not allowed by this server");
388 if (keySize != 2048 && keySize != 2048 * 2 && keySize != 2048 * 4) {
389 throw ResidueException(
"Invalid key size. Please select 2048, 4096 or 8192");
399 START_EASYLOGGINGPP(argc, argv);
409 START_EASYLOGGINGPP(argc, argv);
424 if (!
clientId.empty() && privateKeyPem.empty()) {
473 el::Helpers::setThreadName(threadName);
480 static inline void enableCrashHandler(
const std::string& loggerId = el::base::consts::kDefaultLoggerId)
482 #if defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_CRASH_LOG) 484 el::Helpers::setCrashHandler(residueCrashHandler);
493 static inline const std::vector<std::string>&
errors() noexcept
541 static inline std::string connection()
578 return m_crashHandlerLoggerId;
592 inline std::string serverVersion() const noexcept
594 return m_serverVersion;
602 std::string threadId;
603 std::string loggerId;
604 el::base::type::string_t msg;
606 el::base::type::LineNumber line;
609 el::base::type::VerboseLevel vlevel;
617 std::string m_connection;
619 std::atomic<bool> m_connected;
620 std::atomic<bool> m_connecting;
621 std::atomic<bool> m_disconnected;
623 std::atomic<bool> m_bulkDispatch;
624 unsigned int m_bulkSize;
625 unsigned int m_maxBulkSize;
627 std::string m_rsaPrivateKey;
628 std::string m_rsaPrivateKeySecret;
629 std::string m_rsaPublicKey;
630 std::size_t m_keySize;
631 std::string m_clientId;
633 std::string m_serverVersion;
634 std::string m_serverPublicKey;
636 unsigned long m_dateCreated;
638 unsigned int m_serverFlags;
639 std::atomic<bool> m_utc;
640 std::atomic<int> m_timeOffset;
641 std::atomic<unsigned int> m_dispatchDelay;
643 std::deque<RawRequest> m_requests;
645 std::recursive_mutex m_mutex;
646 std::thread* m_dispatcher;
647 std::atomic<bool> m_running;
648 std::atomic<bool> m_autoBulkParams;
650 std::string m_applicationId;
653 std::string m_homepath;
655 std::string m_crashHandlerLoggerId;
657 std::vector<std::string> m_errors;
658 std::mutex m_errorsMutex;
660 friend class ResidueDispatcher;
665 bool isClientValid() const noexcept;
666 bool shouldTouch() const noexcept;
667 void touch() noexcept;
668 void healthCheck() noexcept;
671 void connect_(const std::
string& host,
int port,
bool estabilishFullConnection = true);
672 void disconnect_() noexcept;
674 void onConnect() noexcept;
675 void loadConnectionFromJson_(const std::
string& connectionJson);
676 void saveConnection_(const std::
string& outputFile);
677 void loadConnection_(const std::
string& connectionFile);
679 void loadConfigurationFromJson_(const std::
string& json);
682 void addToQueue(RawRequest&&) noexcept;
684 std::
string requestToJson(RawRequest&& request);
686 void addError(const std::
string& errorText) noexcept;
688 std::
string& resolveResidueHomeEnvVar(std::
string& str);
static void loadConfigurationFromJson(const std::string &json)
Loads configuration from JSON.
Definition: residue.h:527
static const std::string LOCALHOST
127.0.0.1
Definition: residue.h:97
static void setKeySize(std::size_t keySize)
Size of RSA key (in bits) when connecting.
Definition: residue.h:386
static void setBulkSize(unsigned int bulkSize)
Sets number of log messages to be bulked together.
Definition: residue.h:339
static void connect(int port)
Connect to residue on localhost.
Definition: residue.h:151
static void setApplicationArgs(int argc, const char **argv)
Wrapper for START_EASYLOGGINGPP.
Definition: residue.h:407
Exception thrown by all the residue helper and internal methods.
Definition: residue.h:36
static bool connecting() noexcept
Check connection to the server.
Definition: residue.h:211
static const std::size_t DEFAULT_KEY_SIZE
Default key size for RSA.
Definition: residue.h:87
static void setApplicationArgs(int argc, char **argv)
Definition: residue.h:397
static unsigned int serverFlags() noexcept
If connected this identifies server flags.
Definition: residue.h:228
static const int DEFAULT_PORT
Default port 8777 of residue.
Definition: residue.h:92
static const unsigned int TOUCH_THRESHOLD
Threshold (seconds) to check against client age and whether we should send touch request or not...
Definition: residue.h:103
static void setResidueHomePath(const std::string &path) noexcept
Sets residue home path for $RESIDUE_HOME.
Definition: residue.h:261
static void enableAutoBulkParams() noexcept
Enables automatic setting of bulk parameters depending on what's most efficient. It essentially enabl...
Definition: residue.h:355
static void setKnownClient(const std::string &clientId, const std::string &privateKeyPem)
If server knows you already you can let us know and we will use this identity.
Definition: residue.h:420
static void loadConnection(const std::string &connectionFile)
Loads connection from file instead of re-pulling it from server.
Definition: residue.h:559
The Residue class provides helper methods to connect and interact to residue server seamlessly...
Definition: residue.h:55
static void enableCrashHandler(const std::string &loggerId=el::base::consts::kDefaultLoggerId)
Enables crash handler.
Definition: residue.h:480
static void connect()
Connect to residue on localhost using default port. This is more useful when you have client ID so yo...
Definition: residue.h:173
std::string crashHandlerLoggerId() const noexcept
Crash handler logger. Not for public use.
Definition: residue.h:576
unsigned long getTimestamp() const noexcept
Gets timestamp for replay attack prevention. This function always uses unix-time for comparison...
Definition: residue.h:585
static void setApplicationId(const std::string &id) noexcept
Application ID is what gets passed on to the server for app format specifier.
Definition: residue.h:253
Flag
Server flags enum that are relavant to the client.
Definition: residue.h:60
static std::string info() noexcept
Version of Residue library and dependencies.
static void disableBulkDispatch() noexcept
disableBulkDispatch turns off bulk dispatch.
Definition: residue.h:282
static void loadConnectionFromJson(const std::string &connectionJson)
Loads configuration from JSON.
static void disconnect() noexcept
Safely disconnects from the server. This will also call wait()
static void setInternalLoggingLevel(InternalLoggingLevel level)
Helper function to set logging level for debugging/info.
Definition: residue.h:455
static void connect(const std::string &host)
Connect to residue using default port.
Definition: residue.h:161
ResidueException(const std::string &msg)
Main constructor.
Definition: residue.h:42
static void setInternalLoggingLevel(int level)
Sets logging level for residue client library internal logging.
Definition: residue.h:446
static void setServerPublicKey(const std::string &publicKeyPem)
Sets server public key for encrypted connection.
Definition: residue.h:434
static const std::vector< std::string > & errors() noexcept
Useful for debugging the issue. Contains last 10 errors from server (i.e, error_text in response) Las...
Definition: residue.h:493
static void setTimeOffset(int offset) noexcept
Offset in seconds for log time. [Default: 0].
Definition: residue.h:306
static std::string version() noexcept
Version of Residue library.
static unsigned int maxBulkSize() noexcept
If connected this identifies maximum number of items in bulk that server accepts. ...
Definition: residue.h:220
static void enableUtc() noexcept
Send UTC time to the server. By default local time is sent.
Definition: residue.h:290
static void disableAutoBulkParams() noexcept
Disables automatic setting of bulk params.
Definition: residue.h:366
static void wait() noexcept
Wait to dispatch all the log requests to the server.
Definition: residue.h:243
static void setDispatchDelay(unsigned int delay) noexcept
Delay between dispatching log messages (in milliseconds) [Default: 1ms].
Definition: residue.h:328
static volatile int s_internalLoggingLevel
See setInternalLoggingLevel(InternalLoggingLevel)
Definition: residue.h:108
static void disableUtc() noexcept
Send Local time to the server. By default local time is sent.
Definition: residue.h:298
static const std::string & clientId() noexcept
Returns current client ID. Returns empty if not connected.
Definition: residue.h:202
static void loadConfiguration(const std::string &jsonFilename)
Loads all the configurations from JSON file.
static void enableBulkDispatch()
enableBulkDispatch turns on bulk dispatch.
Definition: residue.h:271
static void reconnect(const std::string &host="", int port=-1)
Reconnects residue server using new or initially provided parameters. (Disconnect then connect) ...
Definition: residue.h:183
static bool hasFlag(Flag flag) noexcept
Checks for server flag and returns true if flag set otherwise not.
Definition: residue.h:463
static Residue & instance() noexcept
Singleton instance.
Definition: residue.h:113
static bool connected() noexcept
Check connection to the server.
Definition: residue.h:194
static void saveConnection(const std::string &outputFile)
Saves connection parameter to the file.
Definition: residue.h:536
static void setThreadName(const std::string &threadName) noexcept
Sets current thread name.
Definition: residue.h:471