From e7fe11742621d42bef0b69bf8f22fe2d2c693b9b Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Sun, 6 Dec 2015 12:36:12 +0100 Subject: [PATCH] don't log full file paths, remove use of KJ_DBG --- src/laminar.cpp | 22 +++++++++++----------- src/log.h | 37 +++++++++++++++++++++++++++++++++++++ src/main.cpp | 6 +++--- src/run.cpp | 10 +++++----- src/server.cpp | 14 +++++++------- 5 files changed, 63 insertions(+), 26 deletions(-) create mode 100644 src/log.h diff --git a/src/laminar.cpp b/src/laminar.cpp index 8ab9564..c0629db 100644 --- a/src/laminar.cpp +++ b/src/laminar.cpp @@ -19,10 +19,10 @@ #include "laminar.h" #include "server.h" #include "conf.h" +#include "log.h" #include #include -#include #include namespace fs = boost::filesystem; @@ -404,14 +404,14 @@ bool Laminar::loadConfiguration() { std::shared_ptr Laminar::queueJob(std::string name, ParamMap params) { if(!fs::exists(fs::path(homeDir)/"cfg"/"jobs"/name)) { - KJ_LOG(ERROR, "Non-existent job", name); + LLOG(ERROR, "Non-existent job", name); return nullptr; } // attempt to create a workspace for this job if it doesn't exist if(!fs::exists(fs::path(homeDir)/"run"/name/"workspace")) { if(!fs::create_directories(fs::path(homeDir)/"run"/name/"workspace")) { - KJ_LOG(ERROR, "Could not create job workspace", name); + LLOG(ERROR, "Could not create job workspace", name); return nullptr; } } @@ -428,7 +428,7 @@ std::shared_ptr Laminar::queueJob(std::string name, ParamMap params) { } else if(it->first == "=reason") { run->reasonMsg = it->second; } else { - KJ_LOG(ERROR, "Unknown internal job parameter", it->first); + LLOG(ERROR, "Unknown internal job parameter", it->first); } it = params.erase(it); } else @@ -486,7 +486,7 @@ void Laminar::reapAdvance() { pid_t pid = waitpid(-1, &ret, 0); // TODO: handle signalled child processes if(pid > 0) { - KJ_LOG(INFO, "Reaping", pid); + LLOG(INFO, "Reaping", pid); auto it = activeJobs.get<0>().find(pid); std::shared_ptr run = *it; bool completed = true; @@ -537,16 +537,16 @@ void Laminar::assignNewJobs() { // create a working directory (different to a workspace!) fs::path wd = fs::path(homeDir)/"run"/run->name/std::to_string(buildNum); if(!fs::create_directory(wd)) { - KJ_LOG(ERROR, "Could not create working directory", wd.string()); + LLOG(ERROR, "Could not create working directory", wd.string()); break; } run->wd = wd.string(); // create an archive directory fs::path archive = fs::path(homeDir)/"archive"/run->name/std::to_string(buildNum); if(fs::is_directory(archive)) { - KJ_LOG(WARNING, "Archive directory already exists", archive.string()); + LLOG(WARNING, "Archive directory already exists", archive.string()); } else if(!fs::create_directories(archive)) { - KJ_LOG(ERROR, "Could not create archive directory", archive.string()); + LLOG(ERROR, "Could not create archive directory", archive.string()); break; } @@ -596,7 +596,7 @@ void Laminar::assignNewJobs() { // update next build number buildNums[run->name] = buildNum; - KJ_LOG(INFO, "Queued job to node", run->name, run->build, node.name); + LLOG(INFO, "Queued job to node", run->name, run->build, node.name); // notify clients Json j; @@ -630,7 +630,7 @@ void Laminar::assignNewJobs() { // trigger the first step of the run if(stepRun(run)) { // should never happen - KJ_LOG(INFO, "No steps for run"); + LLOG(INFO, "No steps for run"); run->complete(); } @@ -651,7 +651,7 @@ void Laminar::runFinished(const Run * r) { Node* node = r->node; node->busyExecutors--; - KJ_LOG(INFO, "Run completed", r->name, to_string(r->result)); + LLOG(INFO, "Run completed", r->name, to_string(r->result)); time_t completedAt = time(0); db->stmt("INSERT INTO builds VALUES(?,?,?,?,?,?,?,?,?,?,?)") .bind(r->name, r->build, node->name, r->queuedAt, r->startedAt, completedAt, int(r->result), diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..1632f62 --- /dev/null +++ b/src/log.h @@ -0,0 +1,37 @@ +/// +/// Copyright 2015 Oliver Giles +/// +/// This file is part of Laminar +/// +/// Laminar is free software: you can redistribute it and/or modify +/// it under the terms of the GNU General Public License as published by +/// the Free Software Foundation, either version 3 of the License, or +/// (at your option) any later version. +/// +/// Laminar 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 General Public License for more details. +/// +/// You should have received a copy of the GNU General Public License +/// along with Laminar. If not, see +/// +#ifndef _LAMINAR_LOG_H_ +#define _LAMINAR_LOG_H_ + +#include + +// Simple override to prevent full paths to source files from +// appearing in log messages. Assumes / is the path separator. +// @see kj/debug.h + +#define _LBASENAME(path) strrchr(path, '/') ? strrchr(path, '/') + 1 : path + +#define LLOG(severity, ...) \ + if (!::kj::_::Debug::shouldLog(::kj::_::Debug::Severity::severity)) {} else \ + ::kj::_::Debug::log(_LBASENAME(__FILE__), __LINE__, ::kj::_::Debug::Severity::severity, \ + #__VA_ARGS__, __VA_ARGS__) + + +#endif // _LAMINAR_LOG_H_ + diff --git a/src/main.cpp b/src/main.cpp index b42385f..3968ead 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,9 +17,9 @@ /// along with Laminar. If not, see /// #include "laminar.h" +#include "log.h" #include -#include std::function sigHandler; static void __sigHandler(int) { sigHandler(); } @@ -34,7 +34,7 @@ int main(int argc, char** argv) { do { Laminar laminar; sigHandler = [&](){ - KJ_LOG(INFO, "Received SIGINT"); + LLOG(INFO, "Received SIGINT"); laminar.stop(); }; signal(SIGINT, &__sigHandler); @@ -43,7 +43,7 @@ int main(int argc, char** argv) { laminar.run(); } while(false); - KJ_DBG("end of main"); + LLOG(INFO, "end of main"); return 0; } diff --git a/src/run.cpp b/src/run.cpp index 0d6998a..8df2615 100644 --- a/src/run.cpp +++ b/src/run.cpp @@ -19,9 +19,9 @@ #include "run.h" #include "node.h" #include "conf.h" -#include -#include +#include "log.h" +#include #include #include @@ -47,7 +47,7 @@ Run::Run() { } Run::~Run() { - KJ_DBG("Run destroyed"); + LLOG(INFO, "Run destroyed"); } std::string Run::reason() const { @@ -103,11 +103,11 @@ bool Run::step() { } printf("[laminar] Executing %s\n", currentScript.c_str()); execl(currentScript.c_str(), currentScript.c_str(), NULL); - KJ_LOG(FATAL, "execl returned", strerror(errno)); + LLOG(FATAL, "execl returned", strerror(errno)); _exit(1); } - KJ_LOG(INFO, "Forked", currentScript, pid); + LLOG(INFO, "Forked", currentScript, pid); close(pfd[1]); fd = pfd[0]; this->pid = pid; diff --git a/src/server.cpp b/src/server.cpp index 22fbae4..a8512b7 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -20,12 +20,12 @@ #include "interface.h" #include "laminar.capnp.h" #include "resources.h" +#include "log.h" #include #include #include #include -#include #include #include @@ -58,9 +58,9 @@ namespace { LaminarCi::JobResult fromRunState(RunState state) { switch(state) { case RunState::SUCCESS: return LaminarCi::JobResult::SUCCESS; - case RunState::FAILED: return LaminarCi::JobResult::FAILED; + case RunState::FAILED: return LaminarCi::JobResult::FAILED; + case RunState::ABORTED: return LaminarCi::JobResult::ABORTED; default: - KJ_DBG("TODO log state", to_string(state)); return LaminarCi::JobResult::UNKNOWN; } } @@ -81,7 +81,7 @@ public: // Start a job, without waiting for it to finish kj::Promise trigger(TriggerContext context) override { std::string jobName = context.getParams().getJobName(); - KJ_LOG(INFO, "RPC trigger", jobName); + LLOG(INFO, "RPC trigger", jobName); ParamMap params; for(auto p : context.getParams().getParams()) { params[p.getName().cStr()] = p.getValue().cStr(); @@ -96,7 +96,7 @@ public: // Start a job and wait for the result kj::Promise start(StartContext context) override { std::string jobName = context.getParams().getJobName(); - KJ_LOG(INFO, "RPC start", jobName); + LLOG(INFO, "RPC start", jobName); ParamMap params; for(auto p : context.getParams().getParams()) { params[p.getName().cStr()] = p.getValue().cStr(); @@ -116,7 +116,7 @@ public: kj::Promise pend(PendContext context) override { std::string jobName = context.getParams().getJobName(); int buildNum = context.getParams().getBuildNum(); - KJ_LOG(INFO, "RPC pend", jobName, buildNum); + LLOG(INFO, "RPC pend", jobName, buildNum); kj::Promise promise = laminar.waitForRun(jobName, buildNum); @@ -129,7 +129,7 @@ public: kj::Promise set(SetContext context) override { std::string jobName = context.getParams().getJobName(); int buildNum = context.getParams().getBuildNum(); - KJ_LOG(INFO, "RPC set", jobName, buildNum); + LLOG(INFO, "RPC set", jobName, buildNum); LaminarCi::MethodResult result = laminar.setParam(jobName, buildNum, context.getParams().getParam().getName(), context.getParams().getParam().getValue())