Add timestamp to logger; move soap result to avoid string copy.

master
Adam Gu 7 years ago
parent 2cc22a80a6
commit 1c861c8f5d

@ -3,25 +3,16 @@
#if WEBCC_ENABLE_LOG #if WEBCC_ENABLE_LOG
#include <cassert> #include <cassert>
#include <chrono>
#include <cstdarg> #include <cstdarg>
#include <mutex> // NOLINT #include <ctime>
#include <mutex>
#include <sstream>
#include <string> #include <string>
#include <thread>
#include "boost/thread.hpp" // For thread ID.
namespace webcc { 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 { struct Logger {
int level; int level;
int modes; int modes;
@ -31,9 +22,55 @@ struct Logger {
// Global logger. // Global logger.
static Logger g_logger{ VERB }; 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) { void LogInit(int level, int modes) {
g_logger.modes = modes; g_logger.modes = modes;
g_logger.level = level; 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, ...) { 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_list va_ptr_console;
va_start(va_ptr_console, format); va_start(va_ptr_console, format);
fprintf(stderr, fprintf(stderr, "%s, %s, %5s, %24s, %4d, ",
"%s, %5s, %24s, %4d, ", GetTimestamp().c_str(), kLevelNames[level], GetThreadID().c_str(),
kLevelNames[level], file, line);
GetThreadId().c_str(),
file,
line);
vfprintf(stderr, format, va_ptr_console); vfprintf(stderr, format, va_ptr_console);
fprintf(stderr, "\n"); fprintf(stderr, "\n");

@ -55,6 +55,7 @@ Error SoapClient::Call(const std::string& operation,
} }
if (!http_client.Request(http_request)) { if (!http_client.Request(http_request)) {
timeout_occurred_ = http_client.timeout_occurred();
return http_client.error(); return http_client.error();
} }
@ -65,7 +66,7 @@ Error SoapClient::Call(const std::string& operation,
return kXmlError; return kXmlError;
} }
*result = soap_response.result(); *result = soap_response.result_moved();
return kNoError; return kNoError;
} }

@ -16,6 +16,8 @@ class SoapClient {
public: public:
virtual ~SoapClient() = default; virtual ~SoapClient() = default;
bool timeout_occurred() const { return timeout_occurred_; }
protected: protected:
SoapClient() = default; SoapClient() = default;
@ -28,6 +30,9 @@ class SoapClient {
// -1 means default timeout (normally 30s) will be used. // -1 means default timeout (normally 30s) will be used.
int timeout_seconds_ = -1; int timeout_seconds_ = -1;
// If the error was caused by timeout or not.
bool timeout_occurred_ = false;
SoapNamespace soapenv_ns_; // SOAP envelope namespace. SoapNamespace soapenv_ns_; // SOAP envelope namespace.
SoapNamespace service_ns_; // Namespace for your web service. SoapNamespace service_ns_; // Namespace for your web service.

@ -18,16 +18,8 @@ class SoapResponse : public SoapMessage {
result_name_ = result_name; result_name_ = result_name;
} }
const std::string& result() const { std::string result_moved() {
return result_; return std::move(result_);
}
void set_result(const std::string& result) {
result_ = result;
}
void set_result(std::string&& result) {
result_ = std::move(result);
} }
protected: protected:

Loading…
Cancel
Save