Add warn log level
[khatus.git] / x5 / khatus.c
index d95390a..bb76507 100644 (file)
@@ -17,6 +17,7 @@
 
 #define debug(args...) if (cfg->log_level >= Debug) {fprintf(stderr, "[debug] " args); fflush(stderr);}
 #define info( args...) if (cfg->log_level >= Info ) {fprintf(stderr, "[info] "  args); fflush(stderr);}
+#define warn( args...) if (cfg->log_level >= Warn ) {fprintf(stderr, "[warn] "  args); fflush(stderr);}
 #define error(args...) if (cfg->log_level >= Error) {fprintf(stderr, "[error] " args); fflush(stderr);}
 #define fatal(args...) {fprintf(stderr, "[fatal] " args); exit(EXIT_FAILURE);}
 #define usage(args...) {print_usage(); fatal("[usage] " args);}
@@ -31,6 +32,7 @@ char *argv0;
 typedef enum LogLevel {
        Nothing,
        Error,
+       Warn,
        Info,
        Debug
 } LogLevel;
@@ -69,24 +71,23 @@ struct Config {
 void
 fifo_print_one(Fifo *f, Config *cfg)
 {
-       info(
-               "Fifo "
-               "{"
-                       " name = %s,"
-                       " fd = %d,"
-                       " width = %d,"
-                       " last_read = %d,"
-                       " ttl = %d,"
-                       " pos = %d,"
-                       " next = %p,"
-               " }\n",
-               f->name,
-               f->fd,
-               f->width,
-               f->last_read,
-               f->ttl,
-               f->pos,
-               f->next
+       info("Fifo "
+           "{"
+           " name = %s,"
+           " fd = %d,"
+           " width = %d,"
+           " last_read = %d,"
+           " ttl = %d,"
+           " pos = %d,"
+           " next = %p,"
+           " }\n",
+           f->name,
+           f->fd,
+           f->width,
+           f->last_read,
+           f->ttl,
+           f->pos,
+           f->next
        );
 }
 
@@ -102,20 +103,20 @@ void
 config_print(Config *cfg)
 {
        info(
-               "Config "
-               "{"
-                       " interval = %d,"
-                       " separator = %s,"
-                       " fifo_count = %d,"
-                       " total_width = %d,"
-                       " log_level = %d,"
-                       " fifos = ..."
-               " }\n",
-               cfg->interval,
-               cfg->separator,
-               cfg->fifo_count,
-               cfg->total_width,
-               cfg->log_level
+           "Config "
+           "{"
+           " interval = %d,"
+           " separator = %s,"
+           " fifo_count = %d,"
+           " total_width = %d,"
+           " log_level = %d,"
+           " fifos = ..."
+           " }\n",
+           cfg->interval,
+           cfg->separator,
+           cfg->fifo_count,
+           cfg->total_width,
+           cfg->log_level
        );
        fifo_print_all(cfg->fifos, cfg);
 }
@@ -134,31 +135,31 @@ print_usage()
 {
        assert(argv0);
        fprintf(
-               stderr,
-               "\n"
-               "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
-               "\n"
-               "  SPEC       = FILE_PATH DATA_WIDTH DATA_TTL\n"
-               "  FILE_PATH  = string\n"
-               "  DATA_WIDTH = int  (* (positive) number of characters *)\n"
-               "  DATA_TTL   = int  (* (positive) number of seconds *)\n"
-               "  OPTION     = -i INTERVAL\n"
-               "             | -s SEPARATOR\n"
-               "             | -x (* Output to X root window *)\n"
-               "             | -l LOG_LEVEL\n"
-               "  SEPARATOR  = string\n"
-               "  INTERVAL   = int  (* (positive) number of seconds *)\n"
-               "  LOG_LEVEL  = int  (* %d through %d *)\n"
-               "\n",
-               argv0,
-               Nothing,
-               Debug
+           stderr,
+           "\n"
+           "Usage: %s [OPTION ...] SPEC [SPEC ...]\n"
+           "\n"
+           "  SPEC       = FILE_PATH DATA_WIDTH DATA_TTL\n"
+           "  FILE_PATH  = string\n"
+           "  DATA_WIDTH = int  (* (positive) number of characters *)\n"
+           "  DATA_TTL   = int  (* (positive) number of seconds *)\n"
+           "  OPTION     = -i INTERVAL\n"
+           "             | -s SEPARATOR\n"
+           "             | -x (* Output to X root window *)\n"
+           "             | -l LOG_LEVEL\n"
+           "  SEPARATOR  = string\n"
+           "  INTERVAL   = int  (* (positive) number of seconds *)\n"
+           "  LOG_LEVEL  = int  (* %d through %d *)\n"
+           "\n",
+           argv0,
+           Nothing,
+           Debug
        );
        fprintf(
-               stderr,
-               "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
-               "\n",
-               argv0
+           stderr,
+           "Example: %s -i 1 /dev/shm/khatus/khatus_sensor_x 4 10\n"
+           "\n",
+           argv0
        );
 }
 
@@ -326,9 +327,10 @@ fifo_read_one(Fifo *f, char *buf, Config *cfg)
        if (current == -1) {
                error("Failed to read: \"%s\". Error: %s\n", f->name, strerror(errno));
                fifo_read_error(f, buf);
-       } else
+       } else {
                while (total++ < f->width)
                        *b++ = ' ';
+       }
        /* TODO Record timestamp read */
        close(f->fd);
        f->fd = -1;
@@ -392,10 +394,10 @@ snooze(struct timespec *t, Config *cfg)
 
        if (result < 0) {
                if (errno == EINTR) {
-                       info(
-                               "nanosleep interrupted. Remainder: "
-                               "{ tv_sec = %ld, tv_nsec = %ld }",
-                               remainder.tv_sec, remainder.tv_nsec);
+                       warn(
+                           "nanosleep interrupted. Remainder: "
+                           "{ tv_sec = %ld, tv_nsec = %ld }",
+                           remainder.tv_sec, remainder.tv_nsec);
                        /* No big deal if we occasionally sleep less,
                         * so not attempting to correct after an interruption.
                         */
@@ -507,8 +509,9 @@ main(int argc, char *argv[])
                        timespecsub(&ti, &td, &tc);
                        debug("snooze YES\n");
                        snooze(&tc, cfg);
-               } else
+               } else {
                        debug("snooze NO\n");
+               }
        }
 
        return EXIT_SUCCESS;
This page took 0.036631 seconds and 4 git commands to generate.