/***************************************************************************** * Copyright [2019] * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *****************************************************************************/ #include #include #include // #include #include #include #include #include #include #ifdef _WIN32 #include #include #else #include #include #endif #include #include #include #include #include #include namespace fs = boost::filesystem; using std::lock_guard; using namespace log4cplus; namespace StarQuant { logger* logger::pinstance_ = nullptr; mutex logger::instancelock_; logger::logger() : logfile(nullptr) { Initialize(); } logger::~logger() { fclose(logfile); } logger& logger::instance() { if (pinstance_ == nullptr) { lock_guard g(instancelock_); if (pinstance_ == nullptr) { pinstance_ = new logger(); } } return *pinstance_; } void logger::Initialize() { string fname; if (CConfig::instance()._mode == RUN_MODE::REPLAY_MODE) { fname = CConfig::instance().logDir() + "/starequant-replay-" + ymd() + ".txt"; } else { fname = CConfig::instance().logDir() + "/starquant-" + ymd() + ".txt"; } logfile = fopen(fname.c_str(), "w"); setvbuf(logfile, nullptr, _IONBF, 0); } void logger::Printf2File(const char *format, ...) { lock_guard g(instancelock_); static char buf[1024 * 2]; string tmp = nowMS(); size_t sz = tmp.size(); strcpy(buf, tmp.c_str()); buf[sz] = ' '; va_list args; va_start(args, format); vsnprintf(buf + sz + 1, 1024 * 2 - sz - 1, format, args); size_t buflen = strlen(buf); fwrite(buf, sizeof(char), buflen, logfile); va_end(args); } static bool configured = false; bool SQLogger::doConfigure(string configureName) { if (!configured) { log4cplus::PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT(configureName)); configured = true; return true; } else { return false; } } std::shared_ptr SQLogger::getLogger(string name) { return std::shared_ptr(new SQLogger(name)); } SQLogger::SQLogger(string name) { doConfigure(CConfig::instance().logconfigfile_); logger = log4cplus::Logger::getInstance(name); } string SQLogger::getConfigFolder() { return CConfig::instance().configDir(); } } // namespace StarQuant