Move logging from c55/log to core/log and use it everywhere
parent
dcd21a17ea
commit
73f5da9636
|
@ -4,7 +4,6 @@ set(SRCS
|
|||
c55/getopt.cpp
|
||||
c55/filesys.cpp
|
||||
c55/os.cpp
|
||||
c55/log.cpp
|
||||
)
|
||||
add_library(c55lib STATIC ${SRCS})
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall")
|
||||
|
|
|
@ -68,6 +68,7 @@ set(SERVER_SRCS
|
|||
src/server/main.cpp
|
||||
src/server/state.cpp
|
||||
src/server/rccpp.cpp
|
||||
src/core/log.cpp
|
||||
src/impl/fs.cpp
|
||||
src/impl/event.cpp
|
||||
src/impl/tcpsocket.cpp
|
||||
|
|
|
@ -38,6 +38,10 @@ Naming:
|
|||
- "name": A string; generally works as an identifier but not necessarily
|
||||
- "id": Numeric id of an instance of something that is not a type
|
||||
|
||||
Logging:
|
||||
- Use core/log.h. Only use stdout directly in case of an interactive command
|
||||
line interface (like printing errors in command line arguments).
|
||||
|
||||
Non-exception throwing and exception-throwing methods
|
||||
-----------------------------------------------------
|
||||
- get_x: Returns NULL or equivalent if not found
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "interface/tcpsocket.h"
|
||||
#include "interface/mutex.h"
|
||||
#include "network/include/api.h"
|
||||
#include "core/log.h"
|
||||
#include <iostream>
|
||||
|
||||
using interface::Event;
|
||||
|
@ -51,21 +52,21 @@ struct Module: public interface::Module, public network::Interface
|
|||
m_server(server),
|
||||
m_listening_socket(interface::createTCPSocket())
|
||||
{
|
||||
std::cout<<"network construct"<<std::endl;
|
||||
log_v(MODULE, "network construct");
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
interface::MutexScope ms(m_interface_mutex);
|
||||
|
||||
std::cout<<"network init"<<std::endl;
|
||||
log_v(MODULE, "network init");
|
||||
m_server->sub_event(this, Event::t("core:start"));
|
||||
m_server->sub_event(this, Event::t("network:listen_event"));
|
||||
}
|
||||
|
||||
~Module()
|
||||
{
|
||||
std::cout<<"network destruct"<<std::endl;
|
||||
log_v(MODULE, "network destruct");
|
||||
}
|
||||
|
||||
void event(const Event::Type &type, const Event::Private *p)
|
||||
|
@ -88,9 +89,9 @@ struct Module: public interface::Module, public network::Interface
|
|||
|
||||
if(!m_listening_socket->bind_fd(address, port) ||
|
||||
!m_listening_socket->listen_fd()){
|
||||
std::cerr<<"Failed to bind to "<<address<<":"<<port<<std::endl;
|
||||
log_i(MODULE, "Failed to bind to %s:%s", cs(address), cs(port));
|
||||
} else {
|
||||
std::cerr<<"Listening at "<<address<<":"<<port<<std::endl;
|
||||
log_i(MODULE, "Listening at %s:%s", cs(address), cs(port));
|
||||
}
|
||||
|
||||
m_server->add_socket_event(m_listening_socket->fd(),
|
||||
|
@ -99,7 +100,7 @@ struct Module: public interface::Module, public network::Interface
|
|||
|
||||
void on_listen_event(const interface::SocketEvent &event)
|
||||
{
|
||||
std::cerr<<"network: on_listen_event(): fd="<<event.fd<<std::endl;
|
||||
log_i(MODULE, "network: on_listen_event(): fd=%i", event.fd);
|
||||
// Create socket
|
||||
sp_<interface::TCPSocket> socket(interface::createTCPSocket());
|
||||
// Accept connection
|
||||
|
@ -125,7 +126,7 @@ struct Module: public interface::Module, public network::Interface
|
|||
|
||||
void send(PeerInfo::Id recipient, const Packet::Type &type, const ss_ &data)
|
||||
{
|
||||
std::cerr<<"network::send()"<<std::endl;
|
||||
log_i(MODULE, "network::send()");
|
||||
interface::MutexScope ms(m_interface_mutex);
|
||||
|
||||
auto it = m_peers.find(recipient);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "log.h"
|
||||
#include "os.h"
|
||||
#include "c55/os.h"
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
|
@ -6,7 +6,7 @@ void* Module::check_interface()
|
|||
{
|
||||
void *i = get_interface();
|
||||
if(!i)
|
||||
throw Exception(ss_()+"Module \""+NAME+"\" has no interface");
|
||||
throw Exception(ss_()+"Module \""+MODULE+"\" has no interface");
|
||||
return i;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,8 +8,8 @@ namespace interface
|
|||
{
|
||||
struct Module
|
||||
{
|
||||
const char *NAME = "(unknown module)";
|
||||
Module(const char *name): NAME(name){}
|
||||
const char *MODULE = "(unknown module)";
|
||||
Module(const char *name): MODULE(name){}
|
||||
virtual ~Module(){};
|
||||
virtual void init() = 0;
|
||||
virtual void event(const Event::Type &type, const Event::Private *p) = 0;
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
#include "core/types.h"
|
||||
#include "core/log.h"
|
||||
#include "server/config.h"
|
||||
#include "server/state.h"
|
||||
#include "interface/server.h"
|
||||
#include <c55/getopt.h>
|
||||
#include <c55/os.h>
|
||||
#include <c55/log.h>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
|
|
@ -1,24 +1,21 @@
|
|||
#include "rccpp.h"
|
||||
#include "interface/server.h"
|
||||
|
||||
#include "core/log.h"
|
||||
#include <c55/filesys.h>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <algorithm>
|
||||
#define MODULE "__rccpp"
|
||||
|
||||
namespace rccpp {
|
||||
|
||||
|
@ -83,7 +80,7 @@ bool CCompiler::compile(const std::string &in_path, const std::string &out_path)
|
|||
command += " -o"+out_path;
|
||||
command += " "+in_path;
|
||||
|
||||
//std::cout<<">>> "<< command<<std::endl;
|
||||
//log_i(MODULE, ">>> %s", cs(command));
|
||||
|
||||
// Fork for compilation.
|
||||
int f = fork();
|
||||
|
@ -101,21 +98,20 @@ bool CCompiler::compile(const std::string &in_path, const std::string &out_path)
|
|||
bool CCompiler::build(const std::string &module_name,
|
||||
const std::string &in_path, const std::string &out_path)
|
||||
{
|
||||
std::cout<<"Building "<<module_name<<": "
|
||||
<<in_path<<" -> "<<out_path<<"... ";
|
||||
log_ni(MODULE, "Building %s: %s -> %s... ", cs(module_name), cs(in_path), cs(out_path));
|
||||
|
||||
std::string out_dir = c55fs::stripFilename(out_path);
|
||||
c55fs::CreateAllDirs(out_dir);
|
||||
|
||||
if(!compile(in_path, out_path)){
|
||||
std::cout<<"Failed!"<<std::endl;
|
||||
log_i(MODULE, "Failed!");
|
||||
return false;
|
||||
}
|
||||
std::cout<<"Success!"<<std::endl;
|
||||
log_i(MODULE, "Success!");
|
||||
|
||||
void *new_module = library_load(out_path.c_str());
|
||||
if(new_module == NULL){
|
||||
std::cout<<"Failed to load compiled library: "<<dlerror()<<std::endl;
|
||||
log_i(MODULE, "Failed to load compiled library: %s", dlerror());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -123,7 +119,7 @@ bool CCompiler::build(const std::string &module_name,
|
|||
RCCPP_Constructor constructor = (RCCPP_Constructor)library_get_address(
|
||||
new_module, constructor_name.c_str());
|
||||
if(constructor == nullptr){
|
||||
std::cout<<constructor_name<<" is missing from the library"<<std::endl;
|
||||
log_i(MODULE, "%s is missing from the library", cs(constructor_name));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
#include "state.h"
|
||||
#include "rccpp.h"
|
||||
#include "config.h"
|
||||
#include "core/log.h"
|
||||
#include "interface/module.h"
|
||||
#include "interface/server.h"
|
||||
#include "interface/event.h"
|
||||
//#include "interface/thread.h"
|
||||
#include "interface/mutex.h"
|
||||
#include <c55/log.h>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#define MODULE "__state"
|
||||
|
||||
using interface::Event;
|
||||
|
||||
|
@ -71,7 +72,7 @@ struct CState: public State, public interface::Server
|
|||
{
|
||||
interface::MutexScope ms(m_modules_mutex);
|
||||
|
||||
std::cerr<<"Loading module "<<module_name<<" from "<<path<<std::endl;
|
||||
log_i(MODULE, "Loading module %s from %s", cs(module_name), cs(path));
|
||||
ss_ build_dst = g_server_config.rccpp_build_path +
|
||||
"/"+module_name+".so";
|
||||
m_compiler->include_directories.push_back(m_modules_path);
|
||||
|
@ -152,7 +153,7 @@ struct CState: public State, public interface::Server
|
|||
}
|
||||
}
|
||||
if(mc0 == nullptr){
|
||||
std::cerr<<"sub_event(): Not a known module"<<std::endl;
|
||||
log_w(MODULE, "sub_event(): Not a known module");
|
||||
return;
|
||||
}
|
||||
interface::MutexScope ms2(m_event_subs_mutex);
|
||||
|
@ -160,11 +161,10 @@ struct CState: public State, public interface::Server
|
|||
m_event_subs.resize(type + 1);
|
||||
sv_<ModuleContainer*> &sublist = m_event_subs[type];
|
||||
if(std::find(sublist.begin(), sublist.end(), mc0) != sublist.end()){
|
||||
std::cerr<<"sub_event(): Already on list: "<<module_name<<std::endl;
|
||||
log_w(MODULE, "sub_event(): Already on list: %s", cs(module_name));
|
||||
return;
|
||||
}
|
||||
std::cerr<<"sub_event(): "<<module_name<<" subscribed to "<<type <<
|
||||
std::endl;
|
||||
log_v(MODULE, "sub_event(): %s subscribed to %zu", cs(module_name), type);
|
||||
sublist.push_back(mc0);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include "interface/server.h"
|
||||
#include "interface/fs.h"
|
||||
#include "interface/event.h"
|
||||
#include <iostream>
|
||||
#include "core/log.h"
|
||||
|
||||
using interface::Event;
|
||||
|
||||
|
@ -16,18 +16,18 @@ struct Module: public interface::Module
|
|||
interface::Module("__loader"),
|
||||
m_server(server)
|
||||
{
|
||||
std::cout<<"__loader construct"<<std::endl;
|
||||
log_v(MODULE, "__loader construct");
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
std::cout<<"__loader init"<<std::endl;
|
||||
log_v(MODULE, "__loader init");
|
||||
m_server->sub_event(this, Event::t("core:load_modules"));
|
||||
}
|
||||
|
||||
~Module()
|
||||
{
|
||||
std::cout<<"__loader destruct"<<std::endl;
|
||||
log_v(MODULE, "__loader destruct");
|
||||
}
|
||||
|
||||
void event(const Event::Type &type, const Event::Private *p)
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
#include "interface/mutex.h"
|
||||
#include "test1/include/api.h"
|
||||
#include "network/include/api.h"
|
||||
#include <iostream>
|
||||
//#include <dlfcn.h>
|
||||
#include "core/log.h"
|
||||
|
||||
using interface::Event;
|
||||
|
||||
|
@ -23,14 +22,14 @@ struct Module: public interface::Module
|
|||
m_server(server),
|
||||
m_EventType_test1_thing(Event::t("test1:thing"))
|
||||
{
|
||||
std::cout<<"test1 construct"<<std::endl;
|
||||
log_v(MODULE, "test1 construct");
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
interface::MutexScope ms(m_interface_mutex);
|
||||
|
||||
std::cout<<"test1 init"<<std::endl;
|
||||
log_v(MODULE, "test1 init");
|
||||
m_server->sub_event(this, Event::t("core:start"));
|
||||
m_server->sub_event(this, m_EventType_test1_thing);
|
||||
m_server->sub_event(this, Event::t("network:new_client"));
|
||||
|
@ -38,7 +37,7 @@ struct Module: public interface::Module
|
|||
|
||||
~Module()
|
||||
{
|
||||
std::cout<<"test1 destruct"<<std::endl;
|
||||
log_v(MODULE, "test1 destruct");
|
||||
}
|
||||
|
||||
void event(const Event::Type &type, const Event::Private *p)
|
||||
|
@ -56,12 +55,12 @@ struct Module: public interface::Module
|
|||
|
||||
void on_thing(const Thing &thing)
|
||||
{
|
||||
std::cout<<"test1.thing: some_data="<<thing.some_data<<std::endl;
|
||||
log_i(MODULE, "test1.thing: some_data=%s", cs(thing.some_data));
|
||||
}
|
||||
|
||||
void on_new_client(const network::NewClient &new_client)
|
||||
{
|
||||
std::cout<<"test1::on_new_client: id="<<new_client.info.id<<std::endl;
|
||||
log_i(MODULE, "test1::on_new_client: id=%zu", new_client.info.id);
|
||||
|
||||
network::Interface *inetwork = network::get_interface(m_server);
|
||||
inetwork->send(new_client.info.id, "test1:dummy", "dummy data");
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include "interface/event.h"
|
||||
#include "interface/mutex.h"
|
||||
#include "test1/include/api.h"
|
||||
#include <iostream>
|
||||
#include "core/log.h"
|
||||
|
||||
using interface::Event;
|
||||
|
||||
|
@ -20,20 +20,20 @@ struct Module: public interface::Module
|
|||
m_server(server),
|
||||
m_EventType_core_start(Event::t("core:start"))
|
||||
{
|
||||
std::cout<<"test2 construct"<<std::endl;
|
||||
log_v(MODULE, "test2 construct");
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
interface::MutexScope ms(m_interface_mutex);
|
||||
|
||||
std::cout<<"test2 init"<<std::endl;
|
||||
log_v(MODULE, "test2 init");
|
||||
m_server->sub_event(this, m_EventType_core_start);
|
||||
}
|
||||
|
||||
~Module()
|
||||
{
|
||||
std::cout<<"test2 destruct"<<std::endl;
|
||||
log_v(MODULE, "test2 destruct");
|
||||
}
|
||||
|
||||
void event(const Event::Type &type, const Event::Private *p)
|
||||
|
@ -47,7 +47,7 @@ struct Module: public interface::Module
|
|||
|
||||
void on_start()
|
||||
{
|
||||
std::cout<<"test2 start(): Calling test1"<<std::endl;
|
||||
log_i(MODULE, "test2 start(): Calling test1");
|
||||
|
||||
{ // Raw
|
||||
Event::Type type = Event::t("test1:thing");
|
||||
|
|
Loading…
Reference in New Issue