changeset 21:21c103c33ac9

pasterd: improve verbosity, closes #2472
author David Demelier <markand@malikania.fr>
date Thu, 06 Feb 2020 20:10:00 +0100
parents 85a22b9abaec
children 8d274b012d28
files config.c database.c http.c log.c log.h pasterd.8.in pasterd.c
diffstat 7 files changed, 82 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/config.c	Thu Feb 06 16:08:26 2020 +0100
+++ b/config.c	Thu Feb 06 20:10:00 2020 +0100
@@ -19,5 +19,7 @@
 #include "config.h"
 
 struct config config = {
-	.themedir = SHAREDIR "/paster/themes/minimal"
+	.databasepath   = VARDIR "/paster/paster.db",
+	.themedir       = SHAREDIR "/paster/themes/minimal",
+	.verbosity      = 1
 };
--- a/database.c	Thu Feb 06 16:08:26 2020 +0100
+++ b/database.c	Thu Feb 06 20:10:00 2020 +0100
@@ -138,20 +138,18 @@
 {
 	assert(path);
 
-	log_debug("opening database: %s\n", path);
+	log_info("database: opening %s", path);
 
 	if (sqlite3_open(path, &db) != SQLITE_OK) {
-		log_warn("unable to open %s: %s\n", path, sqlite3_errmsg(db));
+		log_warn("database: unable to open %s: %s", path, sqlite3_errmsg(db));
 		return false;
 	}
 
 	if (sqlite3_exec(db, sql_init, NULL, NULL, NULL) != SQLITE_OK) {
-		log_warn("unable to initialize %s: %s\n", path, sqlite3_errmsg(db));
+		log_warn("database: unable to initialize %s: %s", path, sqlite3_errmsg(db));
 		return false;
 	}
 
-	log_debug("successfully opened database: %s\n", path);
-
 	return true;
 }
 
@@ -164,6 +162,7 @@
 	sqlite3_stmt *stmt = NULL;
 
 	memset(pastes, 0, *max * sizeof (struct paste));
+	log_debug("database: accessing most recents");
 
 	if (sqlite3_prepare(db, sql_recents, -1, &stmt, NULL) != SQLITE_OK ||
 	    sqlite3_bind_int64(stmt, 1, *max) != SQLITE_OK)
@@ -174,13 +173,15 @@
 	for (; i < *max && sqlite3_step(stmt) == SQLITE_ROW; ++i)
 		convert(stmt, &pastes[i]);
 
+	log_debug("database: found %zu pastes", i);
 	sqlite3_finalize(stmt);
 	*max = i;
 
+
 	return true;
 
 sqlite_err:
-	log_warn("database error (recents): %s\n", sqlite3_errmsg(db));
+	log_warn("database: error (recents): %s\n", sqlite3_errmsg(db));
 
 	if (stmt)
 		sqlite3_finalize(stmt);
@@ -195,6 +196,7 @@
 	assert(uuid);
 
 	memset(paste, 0, sizeof (struct paste));
+	log_debug("database: accessing paste with uuid: %s", uuid);
 
 	sqlite3_stmt* stmt = NULL;
 
@@ -221,7 +223,7 @@
 	if (stmt)
 		sqlite3_finalize(stmt);
 
-	log_warn("database error (get): %s", sqlite3_errmsg(db));
+	log_warn("database: error (get): %s", sqlite3_errmsg(db));
 
 	return false;
 }
@@ -232,9 +234,10 @@
 	assert(paste);
 
 	sqlite3_stmt* stmt = NULL;
+	log_debug("database: creating new paste");
 
 	if (sqlite3_exec(db, "BEGIN EXCLUSIVE TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
-		log_warn("could not lock database: %s\n", sqlite3_errmsg(db));
+		log_warn("database: could not lock database: %s", sqlite3_errmsg(db));
 		return false;
 	}
 
@@ -258,13 +261,13 @@
 	sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
 	sqlite3_finalize(stmt);
 
-	log_debug("new paste (%s) from %s expires in one %lld seconds",
+	log_info("database: new paste (%s) from %s expires in one %lld seconds",
 	    paste->uuid, paste->author, paste->duration);
 
 	return true;
 
 sqlite_err:
-	log_warn("database error (insert): %s", sqlite3_errmsg(db));
+	log_warn("database: error (insert): %s", sqlite3_errmsg(db));
 	sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
 
 	if (stmt)
@@ -279,13 +282,19 @@
 void
 database_clear(void)
 {
+	log_debug("database: clearing deprecated pastes");
+
 	if (sqlite3_exec(db, sql_clear, NULL, NULL, NULL) != SQLITE_OK)
-		log_warn("database error (clear): %s\n", sqlite3_errmsg(db));
+		log_warn("database: error (clear): %s\n", sqlite3_errmsg(db));
 }
 
 void
 database_finish(void)
 {
-	if (db)
+	log_debug("database: closing");
+
+	if (db) {
 		sqlite3_close(db);
+		db = NULL;
+	}
 }
--- a/http.c	Thu Feb 06 16:08:26 2020 +0100
+++ b/http.c	Thu Feb 06 20:10:00 2020 +0100
@@ -823,6 +823,8 @@
 {
 	assert(req);
 
+	log_debug("http: accessing page '%s'", req->path);
+
 	if (req->page == PAGE_LAST)
 		page(req, NULL, KHTTP_404, "404.html");
 	else
--- a/log.c	Thu Feb 06 16:08:26 2020 +0100
+++ b/log.c	Thu Feb 06 20:10:00 2020 +0100
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <syslog.h>
 
+#include "config.h"
 #include "log.h"
 
 static int syslog_levels[] = {
@@ -31,27 +32,30 @@
 void
 log_open(void)
 {
-	openlog("paster", 0, LOG_USER);
+	if (config.verbosity > 0)
+		openlog("paster", 0, LOG_USER);
 }
 
 void
-log_write(enum log_level level,const char *fmt, ...)
+log_write(enum log_level level, const char *fmt, ...)
 {
+	assert(level >= 0 && level <= LOG_LEVEL_WARNING);
 	assert(fmt);
-	assert(level >= 0 && level <= LOG_LEVEL_WARNING);
 
-	va_list ap;
+	if (config.verbosity >= level) {
+		va_list ap;
 
-	va_start(ap, fmt);
-	log_vwrite(level, fmt, ap);
-	va_end(ap);
+		va_start(ap, fmt);
+		log_vwrite(level, fmt, ap);
+		va_end(ap);
+	}
 }
 
 void
 log_vwrite(enum log_level level, const char *fmt, va_list ap)
 {
+	assert(level > 0 && level <= LOG_LEVEL_DEBUG);
 	assert(fmt);
-	assert(level >= 0 && level <= LOG_LEVEL_WARNING);
 
 	char line[BUFSIZ];
 
@@ -62,5 +66,6 @@
 void
 log_finish(void)
 {
-	closelog();
+	if (config.verbosity > 0)
+		closelog();
 }
--- a/log.h	Thu Feb 06 16:08:26 2020 +0100
+++ b/log.h	Thu Feb 06 20:10:00 2020 +0100
@@ -22,11 +22,15 @@
 #include <stdarg.h>
 
 enum log_level {
-	LOG_LEVEL_DEBUG,
+	LOG_LEVEL_WARNING = 1,
 	LOG_LEVEL_INFO,
-	LOG_LEVEL_WARNING
+	LOG_LEVEL_DEBUG
 };
 
+#define log_debug(...)  log_write(LOG_LEVEL_DEBUG, __VA_ARGS__)
+#define log_warn(...)   log_write(LOG_LEVEL_WARNING, __VA_ARGS__)
+#define log_info(...)   log_write(LOG_LEVEL_INFO, __VA_ARGS__)
+
 void
 log_open(void);
 
@@ -36,10 +40,6 @@
 void
 log_vwrite(enum log_level level, const char *fmt, va_list ap);
 
-#define log_debug(...) log_write(LOG_LEVEL_DEBUG, __VA_ARGS__)
-#define log_warn(...) log_write(LOG_LEVEL_WARNING, __VA_ARGS__)
-#define log_info(...) log_write(LOG_LEVEL_INFO, __VA_ARGS__)
-
 void
 log_finish(void);
 
--- a/pasterd.8.in	Thu Feb 06 16:08:26 2020 +0100
+++ b/pasterd.8.in	Thu Feb 06 20:10:00 2020 +0100
@@ -23,7 +23,7 @@
 .\" SYNOPSIS
 .Sh SYNOPSIS
 .Nm
-.Op Fl f
+.Op Fl fqv
 .Op Fl d Ar database-path
 .Op Fl t Ar theme-directory
 .\" DESCRIPTION
@@ -61,6 +61,10 @@
 Specify an alternate path for the database.
 .It Fl t Ar theme-directory
 Specify an alternate directory for the theme.
+.It Fl q
+Do not log through syslog at all.
+.It Fl v
+Increase verbosity level.
 .El
 .\" USAGE
 .Sh USAGE
@@ -74,6 +78,25 @@
 will try to use
 .Pa @VARDIR@/paster/paster.db
 database.
+.\" LOGS
+.Sh LOGS
+The
+.Nm
+utility will log information through syslog unless verbosity is disabled.
+Except at startup where the tool can write to stderr some information if it
+can't continue processing, the tool will never write anything to stdout and
+use syslog only.
+.Pp
+The available verbosity level is defined in the following order:
+.Bd -literal -offset Ds
+none < warnings (default) < info < debug
+.Ed
+.Pp
+Use
+.Fl q
+or
+.Va PASTERD_VERBOSITY=0
+if you want to disable syslog completely.
 .\" USING WITH FASTCGI
 .Sh USING WITH FASTCGI
 The recommended way to use
@@ -171,6 +194,8 @@
 Path to the SQLite database.
 .It Va PASTERD_THEME_DIR No (string)
 Directory containing the theme.
+.It Va PASTERD_VERBOSITY No (number)
+Verbosity level, 0 to disable completely.
 .El
 .\" SEE ALSO
 .Sh SEE ALSO
--- a/pasterd.c	Thu Feb 06 16:08:26 2020 +0100
+++ b/pasterd.c	Thu Feb 06 20:10:00 2020 +0100
@@ -57,7 +57,7 @@
 static noreturn void
 usage(void)
 {
-	fprintf(stderr, "usage: paster [-f] [-d database-path] [-t theme-directory]\n");
+	fprintf(stderr, "usage: paster [-fqv] [-d database-path] [-t theme-directory]\n");
 	exit(1);
 }
  
@@ -75,8 +75,10 @@
 		snprintf(config.databasepath, sizeof (config.databasepath), "%s", value);
 	if ((value = getenv("PASTERD_THEME_DIR")))
 		snprintf(config.themedir, sizeof (config.themedir), "%s", value);
+	if ((value = getenv("PASTERD_VERBOSITY")))
+		config.verbosity = atoi(value);
 
-	while ((opt = getopt(argc, argv, "d:ft:")) != -1) {
+	while ((opt = getopt(argc, argv, "d:ft:qv")) != -1) {
 		switch (opt) {
 		case 'd':
 			snprintf(config.databasepath, sizeof (config.databasepath), "%s", optarg);
@@ -87,6 +89,12 @@
 		case 'f':
 			run = &(http_fcgi_run);
 			break;
+		case 'v':
+			config.verbosity++;
+			break;
+		case 'q':
+			config.verbosity = 0;
+			break;
 		default:
 			usage();
 			break;