Add timestamp to logger; move soap result to avoid string copy.
This commit is contained in:
+54
-20
@@ -3,25 +3,16 @@
|
||||
#if WEBCC_ENABLE_LOG
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstdarg>
|
||||
#include <mutex> // NOLINT
|
||||
#include <ctime>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "boost/thread.hpp" // For thread ID.
|
||||
#include <thread>
|
||||
|
||||
namespace webcc {
|
||||
|
||||
static std::string GetThreadId() {
|
||||
boost::thread::id thread_id = boost::this_thread::get_id();
|
||||
std::stringstream ss;
|
||||
ss << thread_id;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
static const char* kLevelNames[] = {
|
||||
"VERB", "INFO", "WARN", "ERRO", "FATA"
|
||||
};
|
||||
|
||||
struct Logger {
|
||||
int level;
|
||||
int modes;
|
||||
@@ -31,9 +22,55 @@ struct Logger {
|
||||
// Global logger.
|
||||
static Logger g_logger{ VERB };
|
||||
|
||||
static std::thread::id g_main_thread_id;
|
||||
|
||||
static const char* kLevelNames[] = {
|
||||
"VERB", "INFO", "WARN", "ERRO", "FATA"
|
||||
};
|
||||
|
||||
void LogInit(int level, int modes) {
|
||||
g_logger.modes = modes;
|
||||
g_logger.level = level;
|
||||
|
||||
// Suppose LogInit() is called from the main thread.
|
||||
g_main_thread_id = std::this_thread::get_id();
|
||||
}
|
||||
|
||||
static std::string GetTimestamp() {
|
||||
using namespace std::chrono;
|
||||
|
||||
auto now = system_clock::now();
|
||||
std::time_t now_c = system_clock::to_time_t(now);
|
||||
std::tm* now_tm = std::localtime(&now_c);
|
||||
|
||||
char buf[20];
|
||||
std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", now_tm);
|
||||
|
||||
std::string timestamp(buf);
|
||||
|
||||
milliseconds milli_seconds = duration_cast<milliseconds>(
|
||||
now.time_since_epoch());
|
||||
std::string micro_seconds_str = std::to_string(milli_seconds.count() % 1000);
|
||||
while (micro_seconds_str.size() < 3) {
|
||||
micro_seconds_str = "0" + micro_seconds_str;
|
||||
}
|
||||
|
||||
timestamp.append(".");
|
||||
timestamp.append(micro_seconds_str);
|
||||
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
static std::string GetThreadID() {
|
||||
std::thread::id thread_id = std::this_thread::get_id();
|
||||
|
||||
if (thread_id == g_main_thread_id) {
|
||||
return "main";
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
ss << thread_id;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void LogWrite(int level, const char* file, int line, const char* format, ...) {
|
||||
@@ -48,12 +85,9 @@ void LogWrite(int level, const char* file, int line, const char* format, ...) {
|
||||
va_list va_ptr_console;
|
||||
va_start(va_ptr_console, format);
|
||||
|
||||
fprintf(stderr,
|
||||
"%s, %5s, %24s, %4d, ",
|
||||
kLevelNames[level],
|
||||
GetThreadId().c_str(),
|
||||
file,
|
||||
line);
|
||||
fprintf(stderr, "%s, %s, %5s, %24s, %4d, ",
|
||||
GetTimestamp().c_str(), kLevelNames[level], GetThreadID().c_str(),
|
||||
file, line);
|
||||
vfprintf(stderr, format, va_ptr_console);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
|
||||
@@ -55,6 +55,7 @@ Error SoapClient::Call(const std::string& operation,
|
||||
}
|
||||
|
||||
if (!http_client.Request(http_request)) {
|
||||
timeout_occurred_ = http_client.timeout_occurred();
|
||||
return http_client.error();
|
||||
}
|
||||
|
||||
@@ -65,7 +66,7 @@ Error SoapClient::Call(const std::string& operation,
|
||||
return kXmlError;
|
||||
}
|
||||
|
||||
*result = soap_response.result();
|
||||
*result = soap_response.result_moved();
|
||||
|
||||
return kNoError;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ class SoapClient {
|
||||
public:
|
||||
virtual ~SoapClient() = default;
|
||||
|
||||
bool timeout_occurred() const { return timeout_occurred_; }
|
||||
|
||||
protected:
|
||||
SoapClient() = default;
|
||||
|
||||
@@ -28,6 +30,9 @@ class SoapClient {
|
||||
// -1 means default timeout (normally 30s) will be used.
|
||||
int timeout_seconds_ = -1;
|
||||
|
||||
// If the error was caused by timeout or not.
|
||||
bool timeout_occurred_ = false;
|
||||
|
||||
SoapNamespace soapenv_ns_; // SOAP envelope namespace.
|
||||
SoapNamespace service_ns_; // Namespace for your web service.
|
||||
|
||||
|
||||
@@ -18,16 +18,8 @@ class SoapResponse : public SoapMessage {
|
||||
result_name_ = result_name;
|
||||
}
|
||||
|
||||
const std::string& result() const {
|
||||
return result_;
|
||||
}
|
||||
|
||||
void set_result(const std::string& result) {
|
||||
result_ = result;
|
||||
}
|
||||
|
||||
void set_result(std::string&& result) {
|
||||
result_ = std::move(result);
|
||||
std::string result_moved() {
|
||||
return std::move(result_);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user