From ce9b60fc55acd893b58bf84b3f0f7f39cffee079 Mon Sep 17 00:00:00 2001 From: Jeremy Tan Date: Sun, 15 Sep 2013 21:47:32 +0800 Subject: [PATCH] Switch to syslog for logging messages. Updated nginx and rsyslog config files are provided to allow access to the log file from the web server, under the location /api/log. This WILL break nginx if you relied on symlinks to the config file. --- server-configs/README.txt | 5 ++ .../nginx}/README | 0 .../nginx}/fastcgi_params | 0 .../nginx}/mime.types | 0 .../nginx}/sites-enabled/mctxconfig | 33 ++++------- server-configs/rsyslog.d/30-mctxserv.conf | 1 + server/log.c | 58 +++++++++---------- server/main.c | 4 ++ 8 files changed, 49 insertions(+), 52 deletions(-) create mode 100644 server-configs/README.txt rename {nginx-configs => server-configs/nginx}/README (100%) rename {nginx-configs => server-configs/nginx}/fastcgi_params (100%) rename {nginx-configs => server-configs/nginx}/mime.types (100%) rename {nginx-configs => server-configs/nginx}/sites-enabled/mctxconfig (75%) create mode 100644 server-configs/rsyslog.d/30-mctxserv.conf diff --git a/server-configs/README.txt b/server-configs/README.txt new file mode 100644 index 0000000..f7f00ef --- /dev/null +++ b/server-configs/README.txt @@ -0,0 +1,5 @@ +To get syslog and nginx to work correctly: +* Update nginx with the latest config file +* Copy the config file to /etc/rsyslog.d/ +* Restart rsyslog with /etc/init.d/rsyslog restart +* chmod the log file: chmod 644 /var/log/mctxserv.log diff --git a/nginx-configs/README b/server-configs/nginx/README similarity index 100% rename from nginx-configs/README rename to server-configs/nginx/README diff --git a/nginx-configs/fastcgi_params b/server-configs/nginx/fastcgi_params similarity index 100% rename from nginx-configs/fastcgi_params rename to server-configs/nginx/fastcgi_params diff --git a/nginx-configs/mime.types b/server-configs/nginx/mime.types similarity index 100% rename from nginx-configs/mime.types rename to server-configs/nginx/mime.types diff --git a/nginx-configs/sites-enabled/mctxconfig b/server-configs/nginx/sites-enabled/mctxconfig similarity index 75% rename from nginx-configs/sites-enabled/mctxconfig rename to server-configs/nginx/sites-enabled/mctxconfig index 4da2b84..1491152 100644 --- a/nginx-configs/sites-enabled/mctxconfig +++ b/server-configs/nginx/sites-enabled/mctxconfig @@ -1,26 +1,7 @@ -# You may add here your -# server { -# ... -# } -# statements for each of your virtual hosts to this file - -## -# You should look at the following URL's in order to grasp a solid understanding -# of Nginx configuration files in order to fully unleash the power of Nginx. -# http://wiki.nginx.org/Pitfalls -# http://wiki.nginx.org/QuickStart -# http://wiki.nginx.org/Configuration -# -# Generally, you will want to move this file somewhere, and start with a clean -# file but keep this around for reference. Or just disable in sites-enabled. -# -# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. -## - server { listen 80; - listen [::]:80 default_server ipv6only=on; + #Change this to match your root directory root /usr/share/nginx/html; index index.php index.html index.htm; @@ -74,13 +55,21 @@ server { location /api { #Login area location ^~ /api/control { - auth_basic "Restricted Access"; - auth_basic_user_file /usr/share/nginx/access/.htpasswd; + #Uncomment to add back login + #auth_basic "Restricted Access"; + #auth_basic_user_file /usr/share/nginx/access/.htpasswd; fastcgi_pass 127.0.0.1:9005; fastcgi_param DOCUMENT_URI_LOCAL control; include fastcgi_params; } + + #Program log + location ^~ /api/log { + alias /var/log/mctxserv.log; + default_type text/plain; + } + location ~ ^/api/?([^?]*) { fastcgi_pass 127.0.0.1:9005; fastcgi_param DOCUMENT_URI_LOCAL $1; diff --git a/server-configs/rsyslog.d/30-mctxserv.conf b/server-configs/rsyslog.d/30-mctxserv.conf new file mode 100644 index 0000000..8798b6f --- /dev/null +++ b/server-configs/rsyslog.d/30-mctxserv.conf @@ -0,0 +1 @@ +if $programname == 'mctxserv' then /var/log/mctxserv.log diff --git a/server/log.c b/server/log.c index d964e61..d41021f 100644 --- a/server/log.c +++ b/server/log.c @@ -4,24 +4,20 @@ */ -#include -#include - // --- Custom headers --- // #include "common.h" #include "log.h" #include "options.h" -// --- Static variables --- // -static const char * unspecified_funct = "???"; - -// --- Function implementations --- // +#include +#include +#include -//TODO: Migrate to syslog (shouldn't be too hard; these functions basically do what syslog does) -// Note that we will want to have a seperate log as well as syslog; give the user the option to view the log using the GUI +static const char * unspecified_funct = "???"; /** - * Print a message to stderr + * Print a message to stderr and log it via syslog. The message must be + * less than BUFSIZ characters long, or it will be truncated. * @param level - Specify how severe the message is. If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed * @param funct - String indicating the function name from which this function was called. @@ -31,17 +27,23 @@ static const char * unspecified_funct = "???"; */ void LogEx(int level, const char * funct, ...) { + //Todo: consider setlogmask(3) to filter messages const char *fmt; + char buffer[BUFSIZ]; va_list va; + + // Don't print the message unless we need to + if (level > g_options.verbosity) + return; + va_start(va, funct); fmt = va_arg(va, const char*); if (fmt == NULL) // sanity check - FatalEx("Log", "Format string is NULL"); + Fatal("Format string is NULL"); - // Don't print the message unless we need to - if (level > g_options.verbosity) - return; + vsnprintf(buffer, BUFSIZ, fmt, va); + va_end(va); if (funct == NULL) funct = unspecified_funct; @@ -51,31 +53,28 @@ void LogEx(int level, const char * funct, ...) switch (level) { case LOGERR: + level = LOG_ERR; severity = "ERROR"; break; case LOGWARN: + level = LOG_WARNING; severity = "WARNING"; break; case LOGNOTE: + level = LOG_NOTICE; severity = "NOTICE"; break; case LOGINFO: + level = LOG_INFO; severity = "INFO"; break; default: + level = LOG_DEBUG; severity = "DEBUG"; break; } - // Print: Program name, PID, severity string, function name first - fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct); - - // Then pass additional arguments with the format string to vfprintf for printing - vfprintf(stderr, fmt, va); - va_end(va); - - // End log messages with a newline - fprintf(stderr, "\n"); + syslog(level, "%s: %s - %s", severity, funct, buffer); } /** @@ -88,6 +87,7 @@ void LogEx(int level, const char * funct, ...) void FatalEx(const char * funct, ...) { const char *fmt; + char buffer[BUFSIZ]; va_list va; va_start(va, funct); fmt = va_arg(va, const char*); @@ -96,19 +96,17 @@ void FatalEx(const char * funct, ...) { // Fatal error in the Fatal function. // (This really shouldn't happen unless someone does something insanely stupid) - FatalEx("Fatal", "Format string is NULL"); + Fatal("Format string is NULL"); return; // Should never get here } + vsnprintf(buffer, BUFSIZ, fmt,va); + va_end(va); + if (funct == NULL) funct = unspecified_funct; - fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct); - - vfprintf(stderr, fmt, va); - va_end(va); - fprintf(stderr, "\n"); - + syslog(LOG_CRIT, "FATAL: %s - %s", funct, buffer); exit(EXIT_FAILURE); } diff --git a/server/main.c b/server/main.c index 4b36b20..c032eb1 100644 --- a/server/main.c +++ b/server/main.c @@ -11,6 +11,7 @@ #include "control.h" // --- Standard headers --- // +#include // for system logging #include // for signal handling // --- Variable definitions --- // @@ -68,6 +69,9 @@ int main(int argc, char ** argv) { ParseArguments(argc, argv); + //Open the system log + openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER); + Log(LOGINFO, "Server started"); // signal handler //TODO: Make this work /* -- 2.20.1