From a40d15edc39e7c2ba568490f53ef27275ba5f236 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Thu, 8 Mar 2018 14:15:10 +0000 Subject: [PATCH] Switch to libspdlog --- Makefile | 4 +- handler.c | 383 ++++++++++++++++++++++++++------------------------- jsonserver.c | 52 ++++--- log.c | 158 --------------------- log.h | 84 ----------- 5 files changed, 233 insertions(+), 448 deletions(-) delete mode 100644 log.c delete mode 100644 log.h diff --git a/Makefile b/Makefile index 46a3cd4..9b7109f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Dependencies: -# libjsoncpp-dev libconfig++-dev +# libjsoncpp-dev libconfig++-dev libspdlog-dev # # Makefile for a Video Disk Recorder plugin @@ -59,7 +59,7 @@ CXXFLAGS += -fpermissive ### The object files (add further files here): -OBJS = $(PLUGIN).o mongoose.o handler.o log.o +OBJS = $(PLUGIN).o mongoose.o handler.o ### The main target: diff --git a/handler.c b/handler.c index 4b047b1..6cfda16 100644 --- a/handler.c +++ b/handler.c @@ -1,5 +1,17 @@ #include "handler.h" +// Log docs: https://github.com/gabime/spdlog +#include +namespace spd = spdlog; +/* +trace +debug +info +warn +error +critical +*/ + #include #include #include @@ -11,11 +23,10 @@ #include #include -#include "log.h" - int jsonserver_request_handler(struct mg_connection *conn) { - Log* log = Log::getInstance(); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + const struct mg_request_info *request_info = mg_get_request_info(conn); if (strcmp(request_info->uri, "/jsonserver")) return 0; // not for us @@ -24,7 +35,7 @@ int jsonserver_request_handler(struct mg_connection *conn) int wvrl = mg_get_var(request_info->query_string, strlen(request_info->query_string), "req", wvrequest, 20); if (wvrl == -1) { - log->log("JSONServer", Log::ERR, "Could not decode req"); + logger->error("request_handler: Could not decode req"); return 0; } @@ -45,10 +56,10 @@ int jsonserver_request_handler(struct mg_connection *conn) { const char* contentLength = mg_get_header(conn, "Content-Length"); int contentLengthI = atoi(contentLength); - log->log("JSONServer", Log::DEBUG, "POST data content length: %i", contentLengthI); + logger->debug("request_hander: POST data content length: {}", contentLengthI); if (contentLengthI > 10000) { - log->log("JSONServer", Log::DEBUG, "Length > 10000, rejecting"); + logger->error("request_handler: Length > 10000, rejecting"); return 0; } @@ -58,7 +69,7 @@ int jsonserver_request_handler(struct mg_connection *conn) int bytesRead = mg_read(conn, postData, contentLengthI); if (bytesRead != contentLengthI) { - log->log("JSONServer", Log::DEBUG, "Could not read up to contentLength"); + logger->error("request_handler: Could not read up to contentLength"); return 0; } postData[contentLengthI] = '\0'; @@ -108,21 +119,21 @@ int jsonserver_request_handler(struct mg_connection *conn) { if (request_info->status_code == 400) // bad request { - log->log("Mongoose", Log::DEBUG, "400 BAD REQUEST:"); - log->log("Mongoose", Log::DEBUG, request_info->request_method); - log->log("Mongoose", Log::DEBUG, request_info->uri); - log->log("Mongoose", Log::DEBUG, request_info->http_version); - log->log("Mongoose", Log::DEBUG, request_info->query_string); - log->log("Mongoose", Log::DEBUG, request_info->log_message); + logger->debug("Mongoose: 400 BAD REQUEST:"); + logger->debug("Mongoose {}", request_info->request_method); + logger->debug("Mongoose {}", request_info->uri); + logger->debug("Mongoose {}", request_info->http_version); + logger->debug("Mongoose {}", request_info->query_string); + logger->debug("Mongoose {}", request_info->log_message); for (int i = 0; i < request_info->num_headers; i++) { - log->log("Mongoose", Log::DEBUG, "%s: %s", request_info->http_headers[i].name, request_info->http_headers[i].value); + logger->debug("Mongoose: {}: {}", request_info->http_headers[i].name, request_info->http_headers[i].value); } } else { - log->log("Mongoose", Log::DEBUG, request_info->log_message); - log->log("Mongoose", Log::DEBUG, request_info->uri); + logger->debug("Mongoose {}", request_info->log_message); + logger->debug("Mongoose {}", request_info->uri); } return (void*)1; } @@ -139,8 +150,8 @@ int jsonserver_request_handler(struct mg_connection *conn) bool jsonserver_gettime(Json::Value& js) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "gettime"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("get_time"); struct timeval tv; gettimeofday(&tv, NULL); @@ -153,8 +164,8 @@ bool jsonserver_gettime(Json::Value& js) bool jsonserver_diskstats(Json::Value& js) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "diskstats"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("diskstats"); int FreeMB; int UsedMB; @@ -175,8 +186,8 @@ bool jsonserver_diskstats(Json::Value& js) bool jsonserver_reclist(Json::Value& js) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "reclist"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("reclist"); Json::Value jsrecordings(Json::arrayValue); cRecordings Recordings; @@ -204,20 +215,20 @@ bool jsonserver_reclist(Json::Value& js) bool jsonserver_recinfo(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "recinfo"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("recinfo"); char reqfilename[1000]; int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000); if (mgv1 == -1) { - log->log("JSONServer", Log::ERR, "Could not decode filename"); + logger->error("recinfo: Could not decode filename"); js["Result"] = false; js["Error"] = "Could not decode filename"; return true; } - log->log("JSONServer", Log::DEBUG, "%s", reqfilename); + logger->debug("recinfo: {}", reqfilename); cRecordings Recordings; Recordings.Load(); // probably have to do this @@ -225,7 +236,7 @@ bool jsonserver_recinfo(Json::Value& js, const char* postData) if (!recording) { - log->log("JSONServer", Log::ERR, "recinfo found no recording"); + logger->error("recinfo: recinfo found no recording"); js["Result"] = false; return true; } @@ -292,20 +303,20 @@ bool jsonserver_recinfo(Json::Value& js, const char* postData) bool jsonserver_recstop(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "recstop"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("recstop"); char reqfilename[1000]; int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000); if (mgv1 == -1) { - log->log("JSONServer", Log::ERR, "Could not decode filename"); + logger->error("recstop: Could not decode filename"); js["Result"] = false; js["Error"] = "Could not decode filename"; return true; } - log->log("JSONServer", Log::DEBUG, "%s", reqfilename); + logger->debug("recstop: {}", reqfilename); cRecordings Recordings; Recordings.Load(); // probably have to do this @@ -313,7 +324,7 @@ bool jsonserver_recstop(Json::Value& js, const char* postData) if (!recording) { - log->log("JSONServer", Log::ERR, "recstop found no recording"); + logger->error("recstop: recstop found no recording"); js["Result"] = false; return true; } @@ -321,14 +332,14 @@ bool jsonserver_recstop(Json::Value& js, const char* postData) cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName()); if (!rc) { - log->log("JSONServer", Log::ERR, "recstop - not currently recording"); + logger->error("recstop: not currently recording"); js["Result"] = false; return true; } if (Timers.BeingEdited()) { - log->log("JSONServer", Log::ERR, "recstop - timers being edited elsewhere"); + logger->debug("recstop: timers being edited elsewhere"); js["Result"] = false; return true; } @@ -336,7 +347,7 @@ bool jsonserver_recstop(Json::Value& js, const char* postData) cTimer* timer = rc->Timer(); if (!timer) { - log->log("JSONServer", Log::ERR, "recstop - timer not found"); + logger->error("recstop: timer not found"); js["Result"] = false; return true; } @@ -350,20 +361,20 @@ bool jsonserver_recstop(Json::Value& js, const char* postData) bool jsonserver_recdel(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "recdel"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("recdel"); char reqfilename[1000]; int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000); if (mgv1 == -1) { - log->log("JSONServer", Log::ERR, "Could not decode filename"); + logger->error("recdel: Could not decode filename"); js["Result"] = false; js["Error"] = "Could not decode filename"; return true; } - log->log("JSONServer", Log::DEBUG, "%s", reqfilename); + logger->debug("recdel: {}", reqfilename); cRecordings Recordings; Recordings.Load(); // probably have to do this @@ -376,7 +387,7 @@ bool jsonserver_recdel(Json::Value& js, const char* postData) return true; } - log->log("JSONServer", Log::DEBUG, "Deleting recording: %s", recording->Name()); + logger->debug("recdel: Deleting recording: {}", recording->Name()); cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName()); if (rc) { @@ -401,8 +412,8 @@ bool jsonserver_recdel(Json::Value& js, const char* postData) bool jsonserver_recmove(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "recmove"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("recmove"); char* fileNameToMove = NULL; char* requestedNewPath = NULL; @@ -422,7 +433,7 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) if ((mgv1 == -1) || (mgv2 == -1) || !strlen(fileNameToMove) || !strlen(requestedNewPath)) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2); + logger->error("recmove: request mgvs: {} {}", mgv1, mgv2); throw 1; } @@ -434,9 +445,9 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) cRecordControl *rc = cRecordControls::GetRecordControl(recordingObj->FileName()); if (rc) throw 3; - log->log("JSONServer", Log::DEBUG, "moving recording: %s", recordingObj->Name()); - log->log("JSONServer", Log::DEBUG, "moving recording: %s", recordingObj->FileName()); - log->log("JSONServer", Log::DEBUG, "to: %s", requestedNewPath); + logger->debug("recmove: moving recording: {}", recordingObj->Name()); + logger->debug("recmove: moving recording: {}", recordingObj->FileName()); + logger->debug("recmove: to: {}", requestedNewPath); const char* t = recordingObj->FileName(); @@ -447,7 +458,7 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) { if (t[k] == '/') { - log->log("JSONServer", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1); + logger->debug("recmove: l1: {}", strlen(&t[k+1]) + 1); dateDirName = new char[strlen(&t[k+1]) + 1]; strcpy(dateDirName, &t[k+1]); break; @@ -460,7 +471,7 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) { if (t[j] == '/') { - log->log("JSONServer", Log::DEBUG, "l2: %i", k - j); + logger->debug("recmove: l2: {}", k - j); titleDirName = new char[k - j]; memcpy(titleDirName, &t[j+1], k - j - 1); titleDirName[k - j - 1] = '\0'; @@ -477,14 +488,14 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) #endif int vidDirStrLen = strlen(vidDirStr); - log->log("JSONServer", Log::DEBUG, "j = %u, strlenvd = %u", j, vidDirStrLen); + logger->debug("recmove: j = {}, strlenvd = {}", j, vidDirStrLen); if (j > vidDirStrLen) // Rec is in a subfolder now { for(m = j-1; m >= 0; m--) { if (t[m] == '/') { - log->log("JSONServer", Log::DEBUG, "l3: %i", j - m); + logger->debug("recmove: l3: {}", j - m); folderName = new char[j - m]; memcpy(folderName, &t[m+1], j - m - 1); folderName[j - m - 1] = '\0'; @@ -495,21 +506,21 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) ExchangeChars(requestedNewPath, true); - log->log("JSONServer", Log::DEBUG, "datedirname: %s", dateDirName); - log->log("JSONServer", Log::DEBUG, "titledirname: %s", titleDirName); - log->log("JSONServer", Log::DEBUG, "viddir: %s", vidDirStr); - if (folderName) log->log("JSONServer", Log::DEBUG, "folderName: %s", folderName); - log->log("JSONServer", Log::DEBUG, "EC: %s", requestedNewPath); + logger->debug("recmove: datedirname: {}", dateDirName); + logger->debug("recmove: titledirname: {}", titleDirName); + logger->debug("recmove: viddir: {}", vidDirStr); + if (folderName) logger->debug("recmove: folderName: {}", folderName); + logger->debug("recmove: EC: {}", requestedNewPath); // Could be a new path - construct that first and test newContainer = new char[vidDirStrLen + strlen(requestedNewPath) + strlen(titleDirName) + 1]; sprintf(newContainer, "%s%s", vidDirStr, requestedNewPath); - log->log("JSONServer", Log::DEBUG, "NPT: %s", newContainer); + logger->debug("recmove: NPT: {}", newContainer); struct stat dstat; int statret = stat(newContainer, &dstat); if ((statret == -1) && (errno == ENOENT)) // Dir does not exist { - log->log("JSONServer", Log::DEBUG, "new path does not exist (1)"); + logger->debug("recmove: new path does not exist (1)"); int mkdirret = mkdir(newContainer, 0755); if (mkdirret != 0) throw 4; } @@ -522,12 +533,12 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) // New path now created or was there already sprintf(newContainer, "%s%s%s", vidDirStr, requestedNewPath, titleDirName); - log->log("JSONServer", Log::DEBUG, "%s", newContainer); + logger->debug("recmove: {}", newContainer); statret = stat(newContainer, &dstat); if ((statret == -1) && (errno == ENOENT)) // Dir does not exist { - log->log("JSONServer", Log::DEBUG, "new dir does not exist (2)"); + logger->debug("recmove: new dir does not exist (2)"); int mkdirret = mkdir(newContainer, 0755); if (mkdirret != 0) throw 6; } @@ -542,14 +553,14 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1]; sprintf(newDir, "%s/%s", newContainer, dateDirName); - log->log("JSONServer", Log::DEBUG, "doing rename '%s' '%s'", t, newDir); + logger->debug("recmove: doing rename '{}' '{}'", t, newDir); if (rename(t, newDir) != 0) throw 8; // Success. Test for remove old dir containter char* tempOldTitleDir = new char[k+1]; memcpy(tempOldTitleDir, t, k); tempOldTitleDir[k] = '\0'; - log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(tempOldTitleDir), tempOldTitleDir); + logger->debug("recmove: len: {}, cp: {}, strlen: {}, oldtitledir: {}", k+1, k, strlen(tempOldTitleDir), tempOldTitleDir); rmdir(tempOldTitleDir); // can't do anything about a fail result at this point. delete[] tempOldTitleDir; @@ -559,7 +570,7 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) char* tempOldFolderName = new char[j+1]; memcpy(tempOldFolderName, t, j); tempOldFolderName[j] = '\0'; - log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldfoldername: %s", j+1, j, strlen(tempOldFolderName), tempOldFolderName); + logger->debug("recmove: len: {}, cp: {}, strlen: {}, oldfoldername: {}", j+1, j, strlen(tempOldFolderName), tempOldFolderName); /* DESCRIPTION rmdir() deletes a directory, which must be empty. @@ -579,42 +590,42 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) js["Result"] = false; if (e == 1) { - log->log("JSONServer", Log::ERR, "Bad parameters"); + logger->error("recmove: Bad parameters"); js["Error"] = "Bad request parameters"; } else if (e == 2) { - log->log("JSONServer", Log::ERR, "Could not find recording to move"); + logger->error("recmove: Could not find recording to move"); js["Error"] = "Bad filename"; } else if (e == 3) { - log->log("JSONServer", Log::ERR, "Could not move recording, it is still recording"); + logger->error("recmove: Could not move recording, it is still recording"); js["Error"] = "Cannot move recording in progress"; } else if (e == 4) { - log->log("JSONServer", Log::ERR, "Failed to make new dir (1)"); + logger->error("recmove: Failed to make new dir (1)"); js["Error"] = "Failed to create new directory (1)"; } else if (e == 5) { - log->log("JSONServer", Log::ERR, "Something already exists? (1)"); + logger->error("recmove: Something already exists? (1)"); js["Error"] = "Something already exists at the new path (1)"; } else if (e == 6) { - log->log("JSONServer", Log::ERR, "Failed to make new dir (2)"); + logger->error("recmove: Failed to make new dir (2)"); js["Error"] = "Failed to create new directory (2)"; } else if (e == 7) { - log->log("JSONServer", Log::ERR, "Something already exists?"); + logger->error("recmove: Something already exists?"); js["Error"] = "Something already exists at the new path"; } else if (e == 8) { - log->log("JSONServer", Log::ERR, "Rename failed"); + logger->error("recmove: Rename failed"); js["Error"] = "Move failed"; } } @@ -632,8 +643,8 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) bool jsonserver_recrename(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "recrename"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("recrename"); char* fileNameToRename = NULL; char* requestedNewName = NULL; @@ -653,7 +664,7 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) if ((mgv1 == -1) || (mgv2 == -1) || !strlen(fileNameToRename) || !strlen(requestedNewName)) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2); + logger->error("recrename: request mgvs: {} {}", mgv1, mgv2); throw 1; } @@ -665,9 +676,9 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) cRecordControl *rc = cRecordControls::GetRecordControl(recordingObj->FileName()); if (rc) throw 3; - log->log("JSONServer", Log::DEBUG, "renaming recording: %s", recordingObj->Name()); - log->log("JSONServer", Log::DEBUG, "renaming recording: %s", recordingObj->FileName()); - log->log("JSONServer", Log::DEBUG, "to: %s", requestedNewName); + logger->debug("recrename: renaming recording: {}", recordingObj->Name()); + logger->debug("recrename: renaming recording: {}", recordingObj->FileName()); + logger->debug("recrename: to: {}", requestedNewName); const char* t = recordingObj->FileName(); @@ -678,7 +689,7 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) { if (t[k] == '/') { - log->log("JSONServer", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1); + logger->debug("recrename: l1: {}", strlen(&t[k+1]) + 1); dateDirName = new char[strlen(&t[k+1]) + 1]; strcpy(dateDirName, &t[k+1]); break; @@ -691,7 +702,7 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) { if (t[j] == '/') { - log->log("JSONServer", Log::DEBUG, "l2: %i", k - j); + logger->debug("recrename: l2: {}", k - j); titleDirName = new char[k - j]; memcpy(titleDirName, &t[j+1], k - j - 1); titleDirName[k - j - 1] = '\0'; @@ -708,14 +719,14 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) #endif int vidDirStrLen = strlen(vidDirStr); - log->log("JSONServer", Log::DEBUG, "j = %u, strlenvd = %u", j, vidDirStrLen); + logger->debug("recrename: j = {}, strlenvd = {}", j, vidDirStrLen); if (j > vidDirStrLen) // Rec is in a subfolder now { for(m = j-1; m >= 0; m--) { if (t[m] == '/') { - log->log("JSONServer", Log::DEBUG, "l3: %i", j - m); + logger->debug("recrename: l3: {}", j - m); folderName = new char[j - m]; memcpy(folderName, &t[m+1], j - m - 1); folderName[j - m - 1] = '\0'; @@ -726,11 +737,11 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) ExchangeChars(requestedNewName, true); - log->log("JSONServer", Log::DEBUG, "datedirname: %s", dateDirName); - log->log("JSONServer", Log::DEBUG, "titledirname: %s", titleDirName); - log->log("JSONServer", Log::DEBUG, "viddir: %s", vidDirStr); - if (folderName) log->log("JSONServer", Log::DEBUG, "folderName: %s", folderName); - log->log("JSONServer", Log::DEBUG, "EC: %s", requestedNewName); + logger->debug("recrename: datedirname: {}", dateDirName); + logger->debug("recrename: titledirname: {}", titleDirName); + logger->debug("recrename: viddir: {}", vidDirStr); + if (folderName) logger->debug("recrename: folderName: {}", folderName); + logger->debug("recrename: EC: {}", requestedNewName); // Could be a new path - construct that first and test @@ -744,12 +755,12 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) newContainer = new char[vidDirStrLen + 1 + strlen(requestedNewName) + 1]; sprintf(newContainer, "%s/%s", vidDirStr, requestedNewName); } - log->log("JSONServer", Log::DEBUG, "NPT: %s", newContainer); + logger->debug("recrename: NPT: {}", newContainer); struct stat dstat; int statret = stat(newContainer, &dstat); if ((statret == -1) && (errno == ENOENT)) // Dir does not exist { - log->log("JSONServer", Log::DEBUG, "new path does not exist (1)"); + logger->debug("recrename: new path does not exist (1)"); int mkdirret = mkdir(newContainer, 0755); if (mkdirret != 0) throw 4; } @@ -764,14 +775,14 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1]; sprintf(newDir, "%s/%s", newContainer, dateDirName); - log->log("JSONServer", Log::DEBUG, "doing rename '%s' '%s'", t, newDir); + logger->debug("recrename: doing rename '{}' '{}'", t, newDir); if (rename(t, newDir) != 0) throw 8; // Success. Test for remove old dir containter char* tempOldTitleDir = new char[k+1]; memcpy(tempOldTitleDir, t, k); tempOldTitleDir[k] = '\0'; - log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(tempOldTitleDir), tempOldTitleDir); + logger->debug("recrename: len: {}, cp: {}, strlen: {}, oldtitledir: {}", k+1, k, strlen(tempOldTitleDir), tempOldTitleDir); rmdir(tempOldTitleDir); // can't do anything about a fail result at this point. delete[] tempOldTitleDir; @@ -784,32 +795,32 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) js["Result"] = false; if (e == 1) { - log->log("JSONServer", Log::ERR, "Bad parameters"); + logger->error("recrename: Bad parameters"); js["Error"] = "Bad request parameters"; } else if (e == 2) { - log->log("JSONServer", Log::ERR, "Could not find recording to move"); + logger->error("recrename: Could not find recording to move"); js["Error"] = "Bad filename"; } else if (e == 3) { - log->log("JSONServer", Log::ERR, "Could not move recording, it is still recording"); + logger->error("recrename: Could not move recording, it is still recording"); js["Error"] = "Cannot move recording in progress"; } else if (e == 4) { - log->log("JSONServer", Log::ERR, "Failed to make new dir (1)"); + logger->error("recrename: Failed to make new dir (1)"); js["Error"] = "Failed to create new directory (1)"; } else if (e == 5) { - log->log("JSONServer", Log::ERR, "Something already exists? (1)"); + logger->error("recrename: Something already exists? (1)"); js["Error"] = "Something already exists at the new path (1)"; } else if (e == 8) { - log->log("JSONServer", Log::ERR, "Rename failed"); + logger->error("recrename: Rename failed"); js["Error"] = "Move failed"; } } @@ -827,20 +838,20 @@ bool jsonserver_recrename(Json::Value& js, const char* postData) bool jsonserver_recresetresume(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "recresetresume"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("recresetresume"); char reqfilename[1000]; int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000); if (mgv1 == -1) { - log->log("JSONServer", Log::ERR, "Could not decode filename"); + logger->error("recresetresume: Could not decode filename"); js["Result"] = false; js["Error"] = "Could not decode filename"; return true; } - log->log("JSONServer", Log::DEBUG, "%s", reqfilename); + logger->debug("recresetresume: {}", reqfilename); cRecordings Recordings; Recordings.Load(); // probably have to do this @@ -853,7 +864,7 @@ bool jsonserver_recresetresume(Json::Value& js, const char* postData) return true; } - log->log("JSONServer", Log::DEBUG, "Reset resume for: %s", recording->Name()); + logger->debug("recresetresume: Reset resume for: {}", recording->Name()); cResumeFile ResumeFile(recording->FileName(), recording->IsPesRecording()); if (ResumeFile.Read() >= 0) @@ -872,8 +883,8 @@ bool jsonserver_recresetresume(Json::Value& js, const char* postData) bool jsonserver_channellist(Json::Value& js) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "channellist"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("channellist"); Json::Value jschannels(Json::arrayValue); @@ -882,7 +893,7 @@ bool jsonserver_channellist(Json::Value& js) { if (!channel->GroupSep()) { -// log->log("JSONServer", Log::DEBUG, "name: '%s'", channel->Name()); +// logger->debug("channellist: name: '{}'", channel->Name()); // if (channel->Vpid()) type = 1; // else if (channel->Apid(0)) type = 2; @@ -908,8 +919,8 @@ bool jsonserver_channellist(Json::Value& js) bool jsonserver_channelschedule(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "channelschedule '%s'", postData); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("channelschedule: '{}'", postData); char sChannelNumber[15]; int mgv1 = mg_get_var(postData, strlen(postData), "channelnumber", sChannelNumber, 15); char sStartTime[15]; int mgv2 = mg_get_var(postData, strlen(postData), "starttime", sStartTime, 15); @@ -917,7 +928,7 @@ bool jsonserver_channelschedule(Json::Value& js, const char* postData) if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) ) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i", mgv1, mgv2, mgv3); + logger->error("channelschedule: request mgvs: {} {} {}", mgv1, mgv2, mgv3); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; @@ -936,7 +947,7 @@ bool jsonserver_channelschedule(Json::Value& js, const char* postData) if (!channel) { - log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber); + logger->error("channelschedule: Could not find requested channel: {}", channelNumber); js["Result"] = false; js["Error"] = "Could not find channel"; return true; @@ -946,7 +957,7 @@ bool jsonserver_channelschedule(Json::Value& js, const char* postData) const cSchedules *Schedules = cSchedules::Schedules(MutexLock); if (!Schedules) { - log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber); + logger->error("channelschedule: Could not find requested channel: {}", channelNumber); js["Result"] = false; js["Error"] = "Internal schedules error (1)"; return true; @@ -954,7 +965,7 @@ bool jsonserver_channelschedule(Json::Value& js, const char* postData) const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID()); if (!Schedule) { - log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber); + logger->error("channelschedule: Could not find requested channel: {}", channelNumber); js["Result"] = false; js["Error"] = "Internal schedules error (2)"; return true; @@ -989,15 +1000,15 @@ bool jsonserver_channelschedule(Json::Value& js, const char* postData) bool jsonserver_getscheduleevent(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "getscheduleevent '%s'", postData); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("getscheduleevent: '{}'", postData); char sChannelNumber[15]; int mgv1 = mg_get_var(postData, strlen(postData), "channelnumber", sChannelNumber, 15); char sEventID[15]; int mgv2 = mg_get_var(postData, strlen(postData), "eventid", sEventID, 15); if ( (mgv1 == -1) || (mgv2 == -1) ) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2); + logger->error("getscheduleevent: request mgvs: {} {}", mgv1, mgv2); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; @@ -1030,7 +1041,7 @@ bool jsonserver_getscheduleevent(Json::Value& js, const char* postData) const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventID, int aroundTime) { - Log* log = Log::getInstance(); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); cChannel* channel = NULL; for (channel = Channels.First(); channel; channel = Channels.Next(channel)) @@ -1041,7 +1052,7 @@ const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventI if (!channel) { - log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber); + logger->error("getevent: Could not find requested channel: {}", channelNumber); js["Error"] = "Could not find channel"; return NULL; } @@ -1050,7 +1061,7 @@ const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventI const cSchedules *Schedules = cSchedules::Schedules(MutexLock); if (!Schedules) { - log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber); + logger->error("getevent: Could not find requested channel: {}", channelNumber); js["Error"] = "Internal schedules error (1)"; return NULL; } @@ -1058,7 +1069,7 @@ const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventI const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID()); if (!Schedule) { - log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber); + logger->error("getevent: Could not find requested channel: {}", channelNumber); js["Error"] = "Internal schedules error (2)"; return NULL; } @@ -1075,7 +1086,7 @@ const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventI if (!event) { - log->log("JSONServer", Log::ERR, "Could not find requested event: %i", eventID); + logger->error("getevent: Could not find requested event: {}", eventID); js["Error"] = "Internal schedules error (3)"; return NULL; } @@ -1085,8 +1096,8 @@ const cEvent* jsonserver_getEvent(Json::Value& js, int channelNumber, int eventI bool jsonserver_timerlist(Json::Value& js) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "timerlist"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("timerlist"); Json::Value jstimers(Json::arrayValue); @@ -1119,7 +1130,7 @@ bool jsonserver_timerlist(Json::Value& js) { int channelNumber = timer->Channel()->Number(); int aroundTime = timer->StartTime() + 1; - log->log("JSONServer", Log::DEBUG, "%i", aroundTime); + logger->debug("timerlist: {}", aroundTime); const cEvent* eventAround = jsonserver_getEvent(js, channelNumber, 0, aroundTime); if (eventAround) @@ -1142,20 +1153,20 @@ bool jsonserver_timerlist(Json::Value& js) bool jsonserver_timerset(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "timerset"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("timerset"); char sTimerString[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "timerstring", sTimerString, 1024); if (mgv1 == -1) { - log->log("JSONServer", Log::ERR, "Could not get timerstring"); + logger->error("timerset: Could not get timerstring"); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } - log->log("JSONServer", Log::DEBUG, "'%s'", sTimerString); + logger->debug("timerset: '{}'", sTimerString); cTimer *timer = new cTimer; if (!timer->Parse(sTimerString)) { @@ -1182,8 +1193,8 @@ bool jsonserver_timerset(Json::Value& js, const char* postData) bool jsonserver_timersetactive(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "timersetactive"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("timersetactive"); char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50); char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024); @@ -1194,13 +1205,13 @@ bool jsonserver_timersetactive(Json::Value& js, const char* postData) if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1) || (mgv6 == -1)) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5, mgv6); + logger->error("timersetactive: request mgvs: {} {} {} {} {} {}", mgv1, mgv2, mgv3, mgv4, mgv5, mgv6); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } - log->log("JSONServer", Log::DEBUG, "timersetactive: %s %s:%s:%s:%s:%s", tNewActive, rChannelID, rName, rStartTime, rStopTime, rWeekDays); + logger->debug("timersetactive: {} {}:{}:{}:{}:{}", tNewActive, rChannelID, rName, rStartTime, rStopTime, rWeekDays); cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays); if (timer) @@ -1234,8 +1245,8 @@ bool jsonserver_timersetactive(Json::Value& js, const char* postData) bool jsonserver_timeredit(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "timeredit"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("timeredit"); // Get all olds char oldName[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "OldName", oldName, 1024); @@ -1250,13 +1261,13 @@ bool jsonserver_timeredit(Json::Value& js, const char* postData) if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1) || (mgv6 == -1) || (mgv7 == -1) || (mgv8 == -1) || (mgv9 == -1)) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5, mgv6, mgv7, mgv8, mgv9); + logger->error("timeredit: request mgvs: {} {} {} {} {} {} {} {} {}", mgv1, mgv2, mgv3, mgv4, mgv5, mgv6, mgv7, mgv8, mgv9); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } - log->log("JSONServer", Log::DEBUG, "timeredit: %s %s %s %s %s %s %s %s %s", oldName, oldActive, oldChannelID, oldDay, oldWeekDays, oldStartTime, oldStopTime, oldPriority, oldLifetime); + logger->debug("timeredit: {} {} {} {} {} {} {} {} {}", oldName, oldActive, oldChannelID, oldDay, oldWeekDays, oldStartTime, oldStopTime, oldPriority, oldLifetime); // Get all news @@ -1272,13 +1283,13 @@ bool jsonserver_timeredit(Json::Value& js, const char* postData) if ( (mgv11 == -1) || (mgv12 == -1) || (mgv13 == -1) || (mgv14 == -1) || (mgv15 == -1) || (mgv16 == -1) || (mgv17 == -1) || (mgv18 == -1) || (mgv19 == -1)) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i %i %i %i %i", mgv11, mgv12, mgv13, mgv14, mgv15, mgv16, mgv17, mgv18, mgv19); + logger->error("timeredit: request mgvs: {} {} {} {} {} {} {} {} {}", mgv11, mgv12, mgv13, mgv14, mgv15, mgv16, mgv17, mgv18, mgv19); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } - log->log("JSONServer", Log::DEBUG, "timeredit: %s %s %s %s %s %s %s %s %s", newName, newActive, newChannelID, newDay, newWeekDays, newStartTime, newStopTime, newPriority, newLifetime); + logger->debug("timeredit: {} {} {} {} {} {} {} {} {}", newName, newActive, newChannelID, newDay, newWeekDays, newStartTime, newStopTime, newPriority, newLifetime); cTimer* timer = jsonserver_findTimer2(oldName, oldActive, oldChannelID, oldDay, oldWeekDays, oldStartTime, oldStopTime, oldPriority, oldLifetime); @@ -1310,7 +1321,7 @@ bool jsonserver_timeredit(Json::Value& js, const char* postData) timer->Flags(), newChannelID, *(cTimer::PrintDay(atoi(newDay), atoi(newWeekDays), true)), nssf, nzsf, atoi(newPriority), atoi(newLifetime), newName, timer->Aux() ? timer->Aux() : ""); - log->log("JSONServer", Log::DEBUG, "timeredit new parse: %s", *parseBuffer); + logger->debug("timeredit: new parse: {}", *parseBuffer); bool parseResult = timer->Parse(*parseBuffer); if (!parseResult) @@ -1322,7 +1333,7 @@ bool jsonserver_timeredit(Json::Value& js, const char* postData) if (timer->HasFlags(tfActive) != !(strcasecmp(newActive, "true"))) { - log->log("JSONServer", Log::DEBUG, "%i %i timeredit set new active: %s", timer->HasFlags(tfActive), !(strcasecmp(newActive, "true")), newActive); + logger->debug("timeredit: {} {} set new active: {}", timer->HasFlags(tfActive), !(strcasecmp(newActive, "true")), newActive); if (strcasecmp(newActive, "true") == 0) { @@ -1348,25 +1359,25 @@ bool jsonserver_timeredit(Json::Value& js, const char* postData) if (strcmp(timer->File(), newName)) { - log->log("JSONServer", Log::DEBUG, "timeredit set new name: %s", newName); + logger->debug("timeredit: set new name: {}", newName); timer->SetFile(newName); } if (timer->Day() != atoi(newDay)) { - log->log("JSONServer", Log::DEBUG, "timeredit set new day: %s", newDay); + logger->debug("timeredit: set new day: {}", newDay); timer->SetDay(atoi(newDay)); } if (timer->WeekDays() != atoi(newWeekDays)) { - log->log("JSONServer", Log::DEBUG, "timeredit set new week days: %s", newWeekDays); + logger->debug("timeredit: set new week days: {}", newWeekDays); timer->SetWeekDays(atoi(newWeekDays)); } if (timer->StartTime() != atoi(newStartTime)) { - log->log("JSONServer", Log::DEBUG, "timeredit set new start time: %s", newStartTime); + logger->debug("timeredit: set new start time: {}", newStartTime); time_t nstt = atoi(newStartTime); struct tm nstm; localtime_r(&nstt, &nstm); @@ -1376,7 +1387,7 @@ bool jsonserver_timeredit(Json::Value& js, const char* postData) if (timer->StopTime() != atoi(newStopTime)) { - log->log("JSONServer", Log::DEBUG, "timeredit set new stop time: %s", newStopTime); + logger->debug("timeredit: set new stop time: {}", newStopTime); time_t nztt = atoi(newStopTime); struct tm nztm; localtime_r(&nztt, &nztm); @@ -1386,13 +1397,13 @@ bool jsonserver_timeredit(Json::Value& js, const char* postData) if (timer->Priority() != atoi(newPriority)) { - log->log("JSONServer", Log::DEBUG, "timeredit set new priority: %s", newPriority); + logger->debug("timeredit: set new priority: {}", newPriority); timer->SetPriority(atoi(newPriority)); } if (timer->Lifetime() != atoi(newLifetime)) { - log->log("JSONServer", Log::DEBUG, "timeredit set new lifetime: %s", newLifetime); + logger->debug("timeredit: set new lifetime: {}", newLifetime); timer->SetLifetime(atoi(newLifetime)); } */ @@ -1400,16 +1411,16 @@ bool jsonserver_timeredit(Json::Value& js, const char* postData) cTimer* jsonserver_findTimer2(const char* rName, const char* rActive, const char* rChannelID, const char* rDay, const char* rWeekDays, const char* rStartTime, const char* rStopTime, const char* rPriority, const char* rLifetime) { - Log* log = Log::getInstance(); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); int numTimers = Timers.Count(); cTimer* timer; - log->log("JSONServer", Log::DEBUG, "findtimer2: %s %s %s %s %s %s %s %s %s", rName, rActive, rChannelID, rDay, rWeekDays, rStartTime, rStopTime, rPriority, rLifetime); + logger->debug("findtimer2: {} {} {} {} {} {} {} {} {}", rName, rActive, rChannelID, rDay, rWeekDays, rStartTime, rStopTime, rPriority, rLifetime); for (int i = 0; i < numTimers; i++) { timer = Timers.Get(i); - log->log("JSONServer", Log::DEBUG, "findtimer2 search: %s %i %s %i %i %i %i %i %i", timer->File(), timer->HasFlags(tfActive), (const char*)timer->Channel()->GetChannelID().ToString(), + logger->debug("findtimer2: search: {} {} {} {} {} {} {} {} {}", timer->File(), timer->HasFlags(tfActive), (const char*)timer->Channel()->GetChannelID().ToString(), (int)timer->Day(), timer->WeekDays(), (int)timer->StartTime(), (int)timer->StopTime(), timer->Priority(), timer->Lifetime()); @@ -1424,17 +1435,17 @@ cTimer* jsonserver_findTimer2(const char* rName, const char* rActive, const char && (timer->Lifetime() == atoi(rLifetime)) ) { - log->log("JSONServer", Log::DEBUG, "found"); + logger->debug("findtimer2: found"); return timer; } } - log->log("JSONServer", Log::DEBUG, "no timer found"); + logger->debug("findtimer2: no timer found"); return NULL; } cTimer* jsonserver_findTimer(const char* rChannelID, const char* rName, const char* rStartTime, const char* rStopTime, const char* rWeekDays) { - Log* log = Log::getInstance(); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); int numTimers = Timers.Count(); cTimer* timer; @@ -1442,17 +1453,17 @@ cTimer* jsonserver_findTimer(const char* rChannelID, const char* rName, const ch { timer = Timers.Get(i); - log->log("JSONServer", Log::DEBUG, "findtimer current: %s", (const char*)timer->ToText(true)); - log->log("JSONServer", Log::DEBUG, "findtimer: %s", (const char*)timer->Channel()->GetChannelID().ToString()); - log->log("JSONServer", Log::DEBUG, "findtimer: %s", rChannelID); - log->log("JSONServer", Log::DEBUG, "findtimer: %s", timer->File()); - log->log("JSONServer", Log::DEBUG, "findtimer: %s", rName); - log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->StartTime()); - log->log("JSONServer", Log::DEBUG, "findtimer: %s", rStartTime); - log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->StopTime()); - log->log("JSONServer", Log::DEBUG, "findtimer: %s", rStopTime); - log->log("JSONServer", Log::DEBUG, "findtimer: %i", timer->WeekDays()); - log->log("JSONServer", Log::DEBUG, "findtimer: %s", rWeekDays); + logger->debug("findtimer: current: {}", (const char*)timer->ToText(true)); + logger->debug("findtimer: {}", (const char*)timer->Channel()->GetChannelID().ToString()); + logger->debug("findtimer: {}", rChannelID); + logger->debug("findtimer: {}", timer->File()); + logger->debug("findtimer: {}", rName); + logger->debug("findtimer: {}", timer->StartTime()); + logger->debug("findtimer: {}", rStartTime); + logger->debug("findtimer: {}", timer->StopTime()); + logger->debug("findtimer: {}", rStopTime); + logger->debug("findtimer: {}", timer->WeekDays()); + logger->debug("findtimer: {}", rWeekDays); if ( (strcmp(timer->Channel()->GetChannelID().ToString(), rChannelID) == 0) @@ -1462,18 +1473,18 @@ cTimer* jsonserver_findTimer(const char* rChannelID, const char* rName, const ch && (timer->WeekDays() == atoi(rWeekDays)) ) { - log->log("JSONServer", Log::DEBUG, "found"); + logger->debug("findtimer: found"); return timer; } } - log->log("JSONServer", Log::DEBUG, "no timer found"); + logger->debug("findtimer: no timer found"); return NULL; } bool jsonserver_timerdel(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "timerdel"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("timerdel"); char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50); char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024); @@ -1483,17 +1494,17 @@ bool jsonserver_timerdel(Json::Value& js, const char* postData) if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1)) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5); + logger->error("timerdel: request mgvs: {} {} {} {} {}", mgv1, mgv2, mgv3, mgv4, mgv5); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } - log->log("JSONServer", Log::DEBUG, "timerdel: %s:%s:%s:%s:%s", rChannelID, rName, rStartTime, rStopTime, rWeekDays); + logger->debug("timerdel: {}:{}:{}:{}:{}", rChannelID, rName, rStartTime, rStopTime, rWeekDays); if (Timers.BeingEdited()) { - log->log("JSONServer", Log::ERR, "Unable to delete timer - timers being edited at VDR"); + logger->debug("timerdel: Unable to delete timer - timers being edited at VDR"); js["Result"] = false; js["Error"] = "Timers being edited at VDR"; return true; @@ -1504,7 +1515,7 @@ bool jsonserver_timerdel(Json::Value& js, const char* postData) { if (timer->Recording()) { - log->log("JSONServer", Log::ERR, "Unable to delete timer - timer is running"); + logger->debug("timerdel: Unable to delete timer - timer is running"); js["Result"] = false; js["Error"] = "Timer is running"; return true; @@ -1525,8 +1536,8 @@ bool jsonserver_timerdel(Json::Value& js, const char* postData) bool jsonserver_timerisrecording(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "timerisrecording"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("timerisrecording"); char rChannelID[50]; int mgv1 = mg_get_var(postData, strlen(postData), "ChannelID", rChannelID, 50); char rName[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "Name", rName, 1024); @@ -1536,13 +1547,13 @@ bool jsonserver_timerisrecording(Json::Value& js, const char* postData) if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1)) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5); + logger->error("timerisrecording: request mgvs: {} {} {} {} {}", mgv1, mgv2, mgv3, mgv4, mgv5); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } - log->log("JSONServer", Log::DEBUG, "timerisrecording: %s:%s:%s:%s:%s", rChannelID, rName, rStartTime, rStopTime, rWeekDays); + logger->debug("timerisrecording: {}:{}:{}:{}:{}", rChannelID, rName, rStartTime, rStopTime, rWeekDays); cTimer* timer = jsonserver_findTimer(rChannelID, rName, rStartTime, rStopTime, rWeekDays); if (timer) @@ -1560,8 +1571,8 @@ bool jsonserver_timerisrecording(Json::Value& js, const char* postData) bool jsonserver_tunersstatus(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "tunerstatus"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("tunerstatus"); js["NumDevices"] = cDevice::NumDevices(); @@ -1647,22 +1658,22 @@ bool jsonserver_tunersstatus(Json::Value& js, const char* postData) bool jsonserver_epgsearchsame(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "epgsearchsame"); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("epgsearchsame"); char sAtTime[15]; int mgv1 = mg_get_var(postData, strlen(postData), "time", sAtTime, 15); char sTitle[1024]; int mgv2 = mg_get_var(postData, strlen(postData), "title", sTitle, 1024); if ( (mgv1 == -1) || (mgv2 == -1) ) { - log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2); + logger->error("epgsearchsame: request mgvs: {} {}", mgv1, mgv2); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } int atTime = atoi(sAtTime); - log->log("JSONServer", Log::DEBUG, "request time: %i, title: %s", atTime, sTitle); + logger->debug("epgsearchsame: request time: {}, title: {}", atTime, sTitle); @@ -1704,15 +1715,15 @@ bool jsonserver_epgsearchsame(Json::Value& js, const char* postData) bool jsonserver_epgdownload(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "channelschedule '%s'", postData); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("epgdownload: '{}'", postData); cSchedulesLock MutexLock; const cSchedules *Schedules = cSchedules::Schedules(MutexLock); if (!Schedules) { - log->log("JSONServer", Log::ERR, "Could not get Schedules object"); + logger->error("epgdownload: Could not get Schedules object"); js["Result"] = false; js["Error"] = "Internal schedules error (1)"; return true; @@ -1751,27 +1762,27 @@ bool jsonserver_epgdownload(Json::Value& js, const char* postData) bool jsonserver_epgsearch(Json::Value& js, const char* postData) { - Log* log = Log::getInstance(); - log->log("JSONServer", Log::DEBUG, "epgsearch '%s'", postData); + std::shared_ptr logger = spd::get("jsonserver_spdlog"); + logger->debug("epgsearch: '{}'", postData); char searchfor[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "searchfor", searchfor, 1024); if (mgv1 == -1) { - log->log("JSONServer", Log::ERR, "request mgvs: %i", mgv1); + logger->error("epgsearch: request mgvs: {}", mgv1); js["Result"] = false; js["Error"] = "Bad request parameters"; return true; } - log->log("JSONServer", Log::DEBUG, "search for: %s", searchfor); + logger->debug("epgsearch: search for: {}", searchfor); cSchedulesLock MutexLock; const cSchedules *Schedules = cSchedules::Schedules(MutexLock); if (!Schedules) { - log->log("JSONServer", Log::ERR, "Could not get Schedules object"); + logger->error("epgsearch: Could not get Schedules object"); js["Result"] = false; js["Error"] = "Internal schedules error (1)"; return true; diff --git a/jsonserver.c b/jsonserver.c index fa4ea8c..caa08b9 100644 --- a/jsonserver.c +++ b/jsonserver.c @@ -4,6 +4,17 @@ * See the README file for copyright information and how to reach the author. * */ +// Log docs: https://github.com/gabime/spdlog +#include +namespace spd = spdlog; +/* +trace +debug +info +warn +error +critical +*/ #include #include @@ -13,7 +24,6 @@ #include "mongoose.h" #include "handler.h" -#include "log.h" static const char *VERSION = "0.0.1"; static const char *DESCRIPTION = "JSON data server plugin for VDR"; @@ -23,7 +33,7 @@ class cPluginJsonserver : public cPlugin { private: // Add any member variables or functions you may need here. struct mg_context *mg; - Log* log; + std::shared_ptr logger; libconfig::Config config; bool mgRunning; public: @@ -55,15 +65,13 @@ cPluginJsonserver::cPluginJsonserver(void) // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT! - log = NULL; mgRunning = false; } cPluginJsonserver::~cPluginJsonserver() { // Clean up after yourself! - if (log) delete log; - log = NULL; + logger.reset(); } const char *cPluginJsonserver::CommandLineHelp(void) @@ -114,9 +122,20 @@ bool cPluginJsonserver::Start(void) std::string cfgLogFilename; if (config.lookupValue("log-file", cfgLogFilename)) { - log = new Log(); - log->init(Log::DEBUG, cfgLogFilename.c_str()); - log->log("Main", Log::INFO, "Logging started"); + try + { + logger = spd::basic_logger_mt("jsonserver_spdlog", cfgLogFilename); + } + catch (const spd::spdlog_ex& ex) + { + dsyslog("jsonserver: Failed to initialise log object: %s", ex.what()); + } + + logger->set_pattern("[%Y-%m-%d %T.%f] [%t] [%l] %v"); + logger->set_level(spd::level::trace); //Set global log level to info + logger->flush_on(spd::level::debug); // FIXME Change this!! + + logger->info("Main: Logging started"); } else { @@ -126,30 +145,27 @@ bool cPluginJsonserver::Start(void) std::string cfgDocRoot; if (!config.lookupValue("doc-root", cfgDocRoot)) { - log->log("Main", Log::CRIT, "Failed to load doc-root from config"); + logger->critical("Main: Failed to load doc-root from config"); dsyslog("jsonserver: Could not load JS App Dir from plugin config file"); - delete log; - log = NULL; + logger.reset(); return false; } std::string cfgPort; if (!config.lookupValue("http-port", cfgPort)) { - log->log("Main", Log::CRIT, "Failed to load http-port from config"); + logger->critical("Main: Failed to load http-port from config"); dsyslog("jsonserver: Could not load http-port from plugin config file"); - delete log; - log = NULL; + logger.reset(); return false; } std::string cfgSSLFilename; if (!config.lookupValue("ssl-pem-file", cfgSSLFilename)) { - log->log("Main", Log::CRIT, "Failed to load ssl-pem-file from config"); + logger->critical("Main: Failed to load ssl-pem-file from config"); dsyslog("jsonserver: Could not load ssl-pem-file from plugin config file"); - delete log; - log = NULL; + logger.reset(); return false; } @@ -169,7 +185,7 @@ bool cPluginJsonserver::Start(void) callbacks.begin_request = jsonserver_request_handler; mg = mg_start(&callbacks, NULL, options); - log->log("JSONServer", Log::INFO, "Mongoose started"); + logger->info("Main: Mongoose started"); mgRunning = true; return true; } diff --git a/log.c b/log.c deleted file mode 100644 index 49a9add..0000000 --- a/log.c +++ /dev/null @@ -1,158 +0,0 @@ -/* - Copyright 2004-2013 Chris Tallon - Copyright 2003-2004 University Of Bradford - - This file is part of VOMP. - - VOMP 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 2 of the License, or - (at your option) any later version. - - VOMP 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 VOMP; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -#include -#include -#include -#include - -#include "log.h" - -Log* Log::instance = NULL; - -Log::Log() -{ - if (instance) return; - instance = this; - logfile = NULL; - initted = 0; - logLevel = 0; -} - -Log::~Log() -{ - instance = NULL; -} - -Log* Log::getInstance() -{ - return instance; -} - -void Log::upLogLevel() -{ - if (!initted) return; - - if (logLevel == Log::DEBUG) - { - log("Log", logLevel, "Log level is at its highest already"); - return; - } - - logLevel++; - log("Log", logLevel, "Log level is now %i", logLevel); -} - -void Log::downLogLevel() -{ - if (!initted) return; - - if (logLevel == Log::CRAZY) - { - log("Log", logLevel, "Log level is at its lowest already"); - return; - } - - logLevel--; - log("Log", logLevel, "Log level is now %i", logLevel); -} - -int Log::init(int startLogLevel, const char* fileName) -{ - logLevel = startLogLevel; - - logfile = fopen(fileName, "a"); - if (logfile) - { - initted = 1; - return 1; - } - else - { - return 0; - } -} - -int Log::shutdown() -{ - if (!initted) return 1; - initted = 0; - if (logfile) fclose(logfile); - return 1; -} - -int Log::log(const char *fromModule, int level, const char* message, ...) -{ - if (!initted) return 0; - - if (level > logLevel) return 1; - - int lineLength = 250; - - char buffer[lineLength + 1]; - int spaceLeft = lineLength; - - struct timeval tv; - gettimeofday(&tv, NULL); - struct tm* tm = localtime(&tv.tv_sec); - spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tm); - spaceLeft -= snprintf(&buffer[lineLength-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec); - - - char levelString[10]; - if (level == CRAZY) strcpy(levelString, "[CRAZY] "); - if (level == EMERG) strcpy(levelString, "[EMERG] "); - if (level == ALERT) strcpy(levelString, "[ALERT] "); - if (level == CRIT) strcpy(levelString, "[CRIT] "); - if (level == ERR) strcpy(levelString, "[ERR] "); - if (level == WARN) strcpy(levelString, "[WARN] "); - if (level == NOTICE) strcpy(levelString, "[notice]"); - if (level == INFO) strcpy(levelString, "[info] "); - if (level == DEBUG) strcpy(levelString, "[debug] "); - - spaceLeft -= snprintf(&buffer[lineLength-spaceLeft], spaceLeft, "%s %s - ", levelString, fromModule); - - va_list ap; - va_start(ap, message); - spaceLeft = vsnprintf(&buffer[lineLength-spaceLeft], spaceLeft, message, ap); - va_end(ap); - - int messageLength = strlen(buffer); - if (messageLength < lineLength) - { - buffer[messageLength] = '\n'; - buffer[messageLength+1] = '\0'; - } - else - { - buffer[lineLength-1] = '\n'; - buffer[lineLength] = '\0'; - } - - int success = fputs(buffer, logfile); - fflush(NULL); - - if (success != EOF) - return 1; - else - return 0; - -} diff --git a/log.h b/log.h deleted file mode 100644 index 50741d2..0000000 --- a/log.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - Copyright 2004-2013 Chris Tallon - Copyright 2003-2004 University Of Bradford - - This file is part of VOMP. - - VOMP 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 2 of the License, or - (at your option) any later version. - - VOMP 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 VOMP; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -#ifndef LOG_H -#define LOG_H - -#include - -class Log -{ - public: - Log(); - ~Log(); - static Log* getInstance(); - - int init(int defaultLevel, const char* fileName); - int shutdown(); - int log(const char *fromModule, int level, const char *message, ...); - void upLogLevel(); - void downLogLevel(); - - const static int CRAZY = 0; // mad crazy things that should never happen - const static int EMERG = 1; // human assist required NOW - const static int ALERT = 2; // system unusable, but happy to sit there - const static int CRIT = 3; // still working, but maybe about to die - const static int ERR = 4; // that response is not even listed... - const static int WARN = 5; // this could be a bad thing. still running tho - const static int NOTICE = 6; // significant good thing - const static int INFO = 7; // verbose good thing - const static int DEBUG = 8; // debug-level messages - - private: - static Log* instance; - int initted; - int logLevel; - int enabled; - - FILE *logfile; -}; - -#endif - -/* - -Documentation -------------- - -This class is intended to be instatiated once by the core. -For a one off use: - -Log::getInstance()->log("", Log::, ""); - -Or, a pointer can be stored and used: - -Log *myptr = Log::getInstance(); - -myptr->log("", Log::, ""); -myptr->log("", Log::, ""); - -Level usages are above. - -The message parameter in the log function can be used in the same way as printf, eg. - -myptr->log("", Log::, "Success: %s %i", stringpointer, integer); - -*/ -- 2.39.5