initial
FossilOrigin-Name: f3dd89e263a7b73e58373f155bef8ca03fbee49dacff56df39d158de1d5ccb0a
This commit is contained in:
parent
377346fa92
commit
7f075a69b1
6 changed files with 282 additions and 0 deletions
21
CMakeLists.txt
Normal file
21
CMakeLists.txt
Normal file
|
@ -0,0 +1,21 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
project(wormhole)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
# C++
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
|
||||
|
||||
include_directories(wormhole include/)
|
||||
|
||||
# files
|
||||
set(sources src/main.cpp
|
||||
src/database/database.cpp
|
||||
src/logger.cpp)
|
||||
|
||||
add_executable(wormhole ${sources})
|
||||
target_link_libraries(wormhole ${CMAKE_DL_LIBS}
|
||||
${CMAKE_THREAD_LIBS_INIT})
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Wormhole
|
||||
|
||||
https://wormhole.nekobit.net/
|
29
src/database/database.h
Normal file
29
src/database/database.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Wormhole - Federated social network
|
||||
* Copyright (C) 2021 Nekobit
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace DB
|
||||
{
|
||||
class Database
|
||||
{
|
||||
public:
|
||||
explicit Database();
|
||||
private:
|
||||
}
|
||||
}
|
72
src/logger.cpp
Normal file
72
src/logger.cpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Wormhole - Federated social network
|
||||
* Copyright (C) 2022 Nekobit
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <utility>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include "logger.hpp"
|
||||
|
||||
using Logger::Level;
|
||||
using Logger::Log;
|
||||
|
||||
Logger::Logger::Logger(bool log_started)
|
||||
: logs{},
|
||||
hook{[](const Log&){}}
|
||||
{
|
||||
if (log_started)
|
||||
*this << "Logging started";
|
||||
}
|
||||
|
||||
/// Returns the time as a std::string in a specified format
|
||||
const std::string Log::pretty_time(const std::string& format) const
|
||||
{
|
||||
std::stringstream time_str;
|
||||
const std::time_t c_time = clock_type::to_time_t(time_point);
|
||||
// time_t doesn't specify it's output, so write into string stream and return that
|
||||
time_str << std::put_time(std::localtime(&c_time), format.c_str());
|
||||
|
||||
return time_str.str();
|
||||
}
|
||||
|
||||
void Logger::Logger::set_log_hook(std::function<void(const Log&)> func)
|
||||
{
|
||||
this->hook = func;
|
||||
}
|
||||
|
||||
/// Creates a log based on the current time
|
||||
Log Logger::Logger::make_log(std::string message, Level level)
|
||||
{
|
||||
auto time = clock_type::now();
|
||||
return { level, std::move(time), std::move(message) };
|
||||
}
|
||||
|
||||
void Logger::Logger::log(std::string message, Level level)
|
||||
{
|
||||
const Log l = make_log(std::move(message), level);
|
||||
logs.emplace_back(l);
|
||||
hook(l);
|
||||
}
|
||||
|
||||
void Logger::Logger::operator<<(std::string message)
|
||||
{
|
||||
const Log l = make_log(std::move(message));
|
||||
logs.emplace_back(l);
|
||||
|
||||
// Call hook
|
||||
hook(l);
|
||||
}
|
119
src/logger.h
Normal file
119
src/logger.h
Normal file
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* Wormhole - Federated social network
|
||||
* Copyright (C) 2022 Nekobit
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// Just your generic, average logger
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iterator>
|
||||
#include <cstddef>
|
||||
|
||||
namespace Logger
|
||||
{
|
||||
using clock_type = typename std::chrono::system_clock;
|
||||
using log_time_point = std::chrono::time_point<clock_type>;
|
||||
|
||||
static const std::string level_string[] = {
|
||||
"Debug",
|
||||
"Info",
|
||||
"Warn",
|
||||
"Error"
|
||||
};
|
||||
|
||||
enum class Level
|
||||
{
|
||||
DEBUG,
|
||||
INFO,
|
||||
WARN,
|
||||
ERROR
|
||||
};
|
||||
|
||||
struct Log
|
||||
{
|
||||
const std::string pretty_time(const std::string& format = "%T") const;
|
||||
|
||||
// Members
|
||||
Logger::Level level;
|
||||
const log_time_point time_point;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
// BEGIN ITERATOR
|
||||
struct Iterator
|
||||
{
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using value_type = Log;
|
||||
|
||||
Iterator(Log* log) : p{log} {}
|
||||
|
||||
// Common operators
|
||||
Log& operator*() const { return *p; }
|
||||
Log* operator->() { return p; }
|
||||
|
||||
// Prefix op
|
||||
Iterator& operator++() { ++p; return *this; }
|
||||
|
||||
// Postfix op
|
||||
Iterator operator++(int) { Iterator t = *this; ++(*this); return t; }
|
||||
|
||||
friend bool operator== (const Iterator& a, const Iterator& b)
|
||||
{ return a.p == b.p; };
|
||||
friend bool operator!= (const Iterator& a, const Iterator& b)
|
||||
{ return a.p != b.p; };
|
||||
|
||||
private:
|
||||
Log* p;
|
||||
};
|
||||
|
||||
// Iterator methods
|
||||
Iterator begin() { return Iterator{&logs[0]}; }
|
||||
Iterator end() { return Iterator{&logs[logs.size()]}; }
|
||||
// END ITERATOR
|
||||
|
||||
Logger(bool log_started = false);
|
||||
~Logger() = default;
|
||||
|
||||
/// Function is called each time a new log is added
|
||||
void set_log_hook(std::function<void(const Log&)> func);
|
||||
|
||||
static Log make_log(std::string message, Level level = Level::INFO);
|
||||
void log(std::string message, Level level = Level::INFO);
|
||||
void operator<<(std::string message);
|
||||
|
||||
private:
|
||||
std::vector<Log> logs;
|
||||
std::function<void(const Log&)> hook;
|
||||
};
|
||||
}
|
||||
|
||||
static std::ostream& operator<<(std::ostream& os, const Logger::Log& log)
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "[" << log.pretty_time()<< "] " <<
|
||||
"<" << Logger::level_string[static_cast<int>(log.level)] << "> " <<
|
||||
log.message;
|
||||
os << stream.str();
|
||||
return os;
|
||||
}
|
38
src/main.cpp
Normal file
38
src/main.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Wormhole - Federated social network
|
||||
* Copyright (C) 2022 Nekobit
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "logger.hpp"
|
||||
|
||||
namespace {
|
||||
void start_wormhole()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// Load wormhole up in another function incase we need to do any global inits here
|
||||
start_wormhole();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in a new issue