2018-08-21 05:27:52 +00:00
|
|
|
// Copyright 2017-2018 ccls Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-05-27 19:24:56 +00:00
|
|
|
#include "log.hh"
|
|
|
|
|
|
|
|
#include <llvm/ADT/SmallString.h>
|
|
|
|
#include <llvm/Support/Threading.h>
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <mutex>
|
2018-05-27 19:24:56 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
namespace ccls::log {
|
|
|
|
static std::mutex mtx;
|
2018-08-09 17:08:14 +00:00
|
|
|
FILE *file;
|
2018-05-27 19:24:56 +00:00
|
|
|
Verbosity verbosity;
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
Message::Message(Verbosity verbosity, const char *file, int line)
|
2018-05-27 19:24:56 +00:00
|
|
|
: verbosity_(verbosity) {
|
|
|
|
using namespace llvm;
|
|
|
|
time_t tim = time(NULL);
|
|
|
|
struct tm t;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
|
|
t = *localtime(&tim);
|
|
|
|
}
|
|
|
|
char buf[16];
|
|
|
|
snprintf(buf, sizeof buf, "%02d:%02d:%02d ", t.tm_hour, t.tm_min, t.tm_sec);
|
|
|
|
stream_ << buf;
|
|
|
|
{
|
|
|
|
SmallString<32> Name;
|
|
|
|
get_thread_name(Name);
|
|
|
|
stream_ << std::left << std::setw(13) << Name.c_str();
|
|
|
|
}
|
|
|
|
{
|
2018-08-09 17:08:14 +00:00
|
|
|
const char *p = strrchr(file, '/');
|
2018-05-27 19:24:56 +00:00
|
|
|
if (p)
|
|
|
|
file = p + 1;
|
|
|
|
stream_ << std::right << std::setw(15) << file << ':' << std::left
|
|
|
|
<< std::setw(3) << line;
|
|
|
|
}
|
|
|
|
stream_ << ' ';
|
|
|
|
// clang-format off
|
|
|
|
switch (verbosity_) {
|
|
|
|
case Verbosity_FATAL: stream_ << 'F'; break;
|
|
|
|
case Verbosity_ERROR: stream_ << 'E'; break;
|
|
|
|
case Verbosity_WARNING: stream_ << 'W'; break;
|
|
|
|
case Verbosity_INFO: stream_ << 'I'; break;
|
|
|
|
default: stream_ << "V(" << int(verbosity_) << ')';
|
|
|
|
}
|
|
|
|
// clang-format on
|
|
|
|
stream_ << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
Message::~Message() {
|
2018-08-09 17:08:14 +00:00
|
|
|
if (!file)
|
|
|
|
return;
|
2018-05-27 19:24:56 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
|
|
stream_ << '\n';
|
|
|
|
fputs(stream_.str().c_str(), file);
|
|
|
|
if (verbosity_ == Verbosity_FATAL)
|
|
|
|
abort();
|
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace ccls::log
|