Skip to content

Commit

Permalink
Replace loguru with a custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Oct 24, 2019
1 parent a6094ef commit cf0d16f
Show file tree
Hide file tree
Showing 40 changed files with 174 additions and 187 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
[submodule "third_party/doctest"]
path = third_party/doctest
url = https://github.com/onqtam/doctest
[submodule "third_party/loguru"]
path = third_party/loguru
url = https://github.com/emilk/loguru
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,15 @@ target_sources(ccls PRIVATE
src/filesystem.cc
src/fuzzy_match.cc
src/main.cc
src/import_pipeline.cc
src/include_complete.cc
src/method.cc
src/language.cc
src/lex_utils.cc
src/log.cc
src/lsp.cc
src/match.cc
src/message_handler.cc
src/pipeline.cc
src/platform_posix.cc
src/platform_win.cc
src/port.cc
Expand Down
2 changes: 0 additions & 2 deletions src/cache_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "lsp.h"
#include "platform.h"

#include <loguru/loguru.hpp>

#include <algorithm>
#include <unordered_map>

Expand Down
20 changes: 11 additions & 9 deletions src/clang_complete.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "clang_complete.h"

#include "clang_utils.h"
#include "filesystem.hh"
#include "log.hh"
#include "platform.h"
#include "timer.h"

#include "filesystem.hh"
#include <llvm/ADT/Twine.h>
#include <llvm/Support/Threading.h>
using namespace llvm;

#include <loguru.hpp>

#include <algorithm>
#include <thread>

Expand Down Expand Up @@ -660,15 +661,15 @@ ClangCompleteManager::ClangCompleteManager(Project* project,
preloaded_sessions_(kMaxPreloadedSessions),
completion_sessions_(kMaxCompletionSessions) {
std::thread([&]() {
SetThreadName("comp-query");
set_thread_name("comp-query");
CompletionQueryMain(this);
}).detach();
std::thread([&]() {
SetThreadName("comp-preload");
set_thread_name("comp-preload");
CompletionPreloadMain(this);
}).detach();
std::thread([&]() {
SetThreadName("diag-query");
set_thread_name("diag-query");
DiagnosticQueryMain(this);
}).detach();
}
Expand Down Expand Up @@ -803,10 +804,11 @@ void ClangCompleteManager::FlushSession(const std::string& filename) {
}

void ClangCompleteManager::FlushAllSessions() {
std::lock_guard<std::mutex> lock(sessions_lock_);
LOG_S(INFO) << "flush all clang complete sessions";
std::lock_guard<std::mutex> lock(sessions_lock_);

preloaded_sessions_.Clear();
completion_sessions_.Clear();
preloaded_sessions_.Clear();
completion_sessions_.Clear();
}

void CodeCompleteCache::WithLock(std::function<void()> action) {
Expand Down
3 changes: 1 addition & 2 deletions src/clang_indexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

#include "clang_cursor.h"
#include "clang_utils.h"
#include "log.hh"
#include "platform.h"
#include "serializer.h"
#include "timer.h"
#include "type_printer.h"

#include <loguru.hpp>

#include <assert.h>
#include <inttypes.h>
#include <limits.h>
Expand Down
3 changes: 1 addition & 2 deletions src/clang_translation_unit.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "clang_translation_unit.h"

#include "clang_utils.h"
#include "log.hh"
#include "platform.h"
#include "utils.h"

#include <loguru.hpp>

namespace {

void EmitDiagnostics(std::string path,
Expand Down
7 changes: 3 additions & 4 deletions src/file_consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

#include "clang_utils.h"
#include "indexer.h"
#include "log.hh"
#include "platform.h"
#include "utils.h"

#include <loguru.hpp>

namespace {

std::optional<std::string> GetFileContents(
Expand Down Expand Up @@ -108,8 +107,8 @@ IndexFile* FileConsumer::TryConsumeFile(
if (clang_getFileUniqueID(file, &file_id) != 0) {
std::string file_name = FileName(file);
if (!file_name.empty()) {
// LOG_S(ERROR) << "Could not get unique file id for " << file_name
// << " when parsing " << parse_file_;
LOG_S(ERROR) << "Could not get unique file id for " << file_name
<< " when parsing " << parse_file_;
}
return nullptr;
}
Expand Down
6 changes: 5 additions & 1 deletion src/include_complete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "project.h"
#include "timer.h"

#include <llvm/ADT/Twine.h>
#include <llvm/Support/Threading.h>
using namespace llvm;

#include <thread>

namespace {
Expand Down Expand Up @@ -103,7 +107,7 @@ void IncludeComplete::Rescan() {

is_scanning = true;
std::thread([this]() {
SetThreadName("scan_includes");
set_thread_name("scan_includes");
Timer timer;

for (const std::string& dir : project_->quote_include_directories)
Expand Down
62 changes: 62 additions & 0 deletions src/log.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "log.hh"

#include <llvm/ADT/SmallString.h>
#include <llvm/Support/Threading.h>

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <iomanip>
#include <mutex>

namespace ccls::log {
static std::mutex mtx;
FILE* file;
Verbosity verbosity;

Message::Message(Verbosity verbosity, const char* file, int line)
: 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();
}
{
const char* p = strrchr(file, '/');
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() {
if (!file) return;
std::lock_guard<std::mutex> lock(mtx);
stream_ << '\n';
fputs(stream_.str().c_str(), file);
if (verbosity_ == Verbosity_FATAL)
abort();
}
}
40 changes: 40 additions & 0 deletions src/log.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <stdio.h>
#include <sstream>

namespace ccls::log {
extern FILE* file;

struct Voidify {
void operator&(const std::ostream&) {}
};

enum Verbosity {
Verbosity_FATAL = -3,
Verbosity_ERROR = -2,
Verbosity_WARNING = -1,
Verbosity_INFO = 0,
};
extern Verbosity verbosity;

struct Message {
std::stringstream stream_;
int verbosity_;

Message(Verbosity verbosity, const char* file, int line);
~Message();
};
}

#define LOG_IF(v, cond) \
!(cond) ? void(0) \
: ccls::log::Voidify() & \
ccls::log::Message(v, __FILE__, __LINE__).stream_
#define LOG_S(v) \
LOG_IF(ccls::log::Verbosity_##v, \
ccls::log::Verbosity_##v <= ccls::log::verbosity)
#define LOG_IF_S(v, cond) \
LOG_IF(ccls::log::Verbosity_##v, \
(cond) && ccls::log::Verbosity_##v <= ccls::log::verbosity)
#define CHECK_S(cond) LOG_IF(FATAL, !(cond)) << "check failed: " #cond " "
42 changes: 1 addition & 41 deletions src/lsp.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "lsp.h"

#include "log.hh"
#include "serializers/json.h"

#include <doctest/doctest.h>
#include <rapidjson/writer.h>
#include <loguru.hpp>

#include <stdio.h>

Expand Down Expand Up @@ -68,45 +67,6 @@ std::optional<std::string> ReadJsonRpcContentFrom(
return content;
}

std::function<std::optional<char>()> MakeContentReader(std::string* content,
bool can_be_empty) {
return [content, can_be_empty]() -> std::optional<char> {
if (!can_be_empty)
REQUIRE(!content->empty());
if (content->empty())
return std::nullopt;
char c = (*content)[0];
content->erase(content->begin());
return c;
};
}

TEST_SUITE("FindIncludeLine") {
TEST_CASE("ReadContentFromSource") {
auto parse_correct = [](std::string content) -> std::string {
auto reader = MakeContentReader(&content, false /*can_be_empty*/);
auto got = ReadJsonRpcContentFrom(reader);
REQUIRE(got);
return got.value();
};

auto parse_incorrect = [](std::string content) -> std::optional<std::string> {
auto reader = MakeContentReader(&content, true /*can_be_empty*/);
return ReadJsonRpcContentFrom(reader);
};

REQUIRE(parse_correct("Content-Length: 0\r\n\r\n") == "");
REQUIRE(parse_correct("Content-Length: 1\r\n\r\na") == "a");
REQUIRE(parse_correct("Content-Length: 4\r\n\r\nabcd") == "abcd");

REQUIRE(parse_incorrect("ggg") == std::optional<std::string>());
REQUIRE(parse_incorrect("Content-Length: 0\r\n") ==
std::optional<std::string>());
REQUIRE(parse_incorrect("Content-Length: 5\r\n\r\nab") ==
std::optional<std::string>());
}
}

std::optional<char> ReadCharFromStdinBlocking() {
// We do not use std::cin because it does not read bytes once stuck in
// cin.bad(). We can call cin.clear() but C++ iostream has other annoyance
Expand Down
8 changes: 0 additions & 8 deletions src/lsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,6 @@ struct lsWorkspaceEdit {
};
MAKE_REFLECT_STRUCT(lsWorkspaceEdit, documentChanges);

struct lsFormattingOptions {
// Size of a tab in spaces.
int tabSize;
// Prefer spaces over tabs.
bool insertSpaces;
};
MAKE_REFLECT_STRUCT(lsFormattingOptions, tabSize, insertSpaces);

// MarkedString can be used to render human readable text. It is either a
// markdown string or a code-block that provides a language and a code snippet.
// The language identifier is sematically equal to the std::optional language
Expand Down
Loading

0 comments on commit cf0d16f

Please sign in to comment.