Przeglądaj źródła

Improve overall logging

Abdullah ibn Nadjo 6 lat temu
rodzic
commit
ccff2b57cd
4 zmienionych plików z 59 dodań i 28 usunięć
  1. 1
    1
      Makefile
  2. 33
    9
      include/helpers.h
  3. 9
    7
      src/helpers.c
  4. 16
    11
      src/light.c

+ 1
- 1
Makefile Wyświetl plik

@@ -3,7 +3,7 @@ BINDIR=$(PREFIX)/bin
3 3
 MANDIR=$(PREFIX)/share/man/man1
4 4
 
5 5
 CC=gcc
6
-CFLAGS=-std=c89 -O2 -pedantic -Wall -I"./include"
6
+CFLAGS=-std=c89 -O2 -pedantic -Wall -I"./include" -D_XOPEN_SOURCE=500
7 7
 MANFLAGS=-h -h -v -V -N
8 8
 
9 9
 HELP2MAN_VERSION := $(shell help2man --version 2>/dev/null)

+ 33
- 9
include/helpers.h Wyświetl plik

@@ -15,20 +15,44 @@
15 15
  * }*/
16 16
 #define LIGHT_CLAMP(x, y, z) ((x<y) ? y : ((x>z) ? z : x ));
17 17
 
18
-#define LIGHT_NOTE(x) if(light_verbosity > 2){printf("%s.\n", x);}
19
-
20
-#define LIGHT_WARN(x) if(light_verbosity > 1){printf("warning: \"%s\", in \"%s\" on line %u.\n", x, __FILE__, __LINE__);}
21
-
22
-#define LIGHT_ERR(x) if(light_verbosity > 0){printf("error: \"%s\", in \"%s\" on line %u.\n", x, __FILE__, __LINE__);}
23
-
24
-#define LIGHT_MEMERR() LIGHT_ERR("memory error");
25
-
18
+#define LIGHT_LOG_FMT_BUF_SIZE 1024
26 19
 /* Verbosity levels: 
27 20
  * 0 - No output
28 21
  * 1 - Errors
29 22
  * 2 - Errors, warnings 
30 23
  * 3 - Errors, warnings, notices */
31
-int    light_verbosity;
24
+typedef enum LIGHT_LOG_LEVEL {
25
+  LIGHT_ERROR_LEVEL = 1,
26
+  LIGHT_WARN_LEVEL,
27
+  LIGHT_NOTE_LEVEL
28
+} LIGHT_LOG_LEVEL;
29
+
30
+LIGHT_LOG_LEVEL light_verbosity;
31
+char light_log_buffer[LIGHT_LOG_FMT_BUF_SIZE];
32
+
33
+#define LIGHT_LOG(lvl,f,t,x)if(light_verbosity >= lvl){fprintf(f,t": \"%s\", in \"%s\" on line %u.\n", x, __FILE__, __LINE__);}
34
+
35
+#define LIGHT_NOTE(x)LIGHT_LOG(LIGHT_NOTE_LEVEL,stdout,"notice",x)
36
+
37
+#define LIGHT_WARN(x)LIGHT_LOG(LIGHT_WARN_LEVEL,stderr,"warning",x)
38
+
39
+#define LIGHT_ERR(x)LIGHT_LOG(LIGHT_ERROR_LEVEL,stderr,"error",x)
40
+
41
+#define LIGHT_LOG_FMT(x,s,f)if(snprintf(light_log_buffer, LIGHT_LOG_FMT_BUF_SIZE,x,s) > 0){f(light_log_buffer);}
42
+
43
+#define LIGHT_NOTE_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_NOTE);
44
+
45
+#define LIGHT_WARN_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_WARN);
46
+
47
+#define LIGHT_ERR_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_ERR);
48
+
49
+#define LIGHT_MEMERR() LIGHT_ERR("memory error");
50
+
51
+#define LIGHT_PERMLOG(x,f)f##_FMT("could not open '%s' for "x,filename); f("check if this file exists or if you have the right permissions");
52
+
53
+#define LIGHT_PERMERR(x) LIGHT_PERMLOG(x,LIGHT_ERR)
54
+
55
+#define LIGHT_PERMWARN(x) LIGHT_PERMLOG(x,LIGHT_WARN)
32 56
 
33 57
 /* Typedef for boolean values */
34 58
 typedef enum LIGHT_BOOL {

+ 9
- 7
src/helpers.c Wyświetl plik

@@ -15,13 +15,13 @@ LIGHT_BOOL light_readUInt(char const * filename, unsigned int *i)
15 15
 
16 16
   if(!fileHandle)
17 17
   {
18
-    LIGHT_ERR("could not open file for reading");
18
+    LIGHT_PERMERR("reading");
19 19
     return FALSE;
20 20
   }
21 21
 
22 22
   if(fscanf(fileHandle, "%u", &iCopy) != 1)
23 23
   {
24
-    LIGHT_ERR("file contents are corrupt");
24
+    LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
25 25
     fclose(fileHandle);
26 26
     return FALSE;
27 27
   }
@@ -40,7 +40,7 @@ LIGHT_BOOL light_writeUInt(char const * filename, unsigned int i)
40 40
 
41 41
   if(!fileHandle)
42 42
   {
43
-    LIGHT_ERR("could not open file for writing");
43
+    LIGHT_PERMERR("writing");
44 44
     return FALSE;
45 45
   }
46 46
 
@@ -65,13 +65,13 @@ LIGHT_BOOL light_readULong(char const * filename, unsigned long *i)
65 65
 
66 66
   if(!fileHandle)
67 67
   {
68
-    LIGHT_ERR("could not open file for reading");
68
+    LIGHT_PERMERR("reading");
69 69
     return FALSE;
70 70
   }
71 71
 
72 72
   if(fscanf(fileHandle, "%lu", &iCopy) != 1)
73 73
   {
74
-    LIGHT_ERR("file contents are corrupt");
74
+    LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
75 75
     fclose(fileHandle);
76 76
     return FALSE;
77 77
   }
@@ -90,7 +90,7 @@ LIGHT_BOOL light_writeULong(char const * filename, unsigned long i)
90 90
 
91 91
   if(!fileHandle)
92 92
   {
93
-    LIGHT_ERR("could not open file for writing");
93
+    LIGHT_PERMERR("writing");
94 94
     return FALSE;
95 95
   }
96 96
 
@@ -125,7 +125,7 @@ LIGHT_BOOL light_readString(char const * filename, char *buffer, long* size)
125 125
 
126 126
   if(!fileHandle)
127 127
   {
128
-    LIGHT_ERR("could not open file for reading");
128
+    LIGHT_PERMERR("reading");
129 129
     return FALSE;
130 130
   }
131 131
 
@@ -186,6 +186,7 @@ LIGHT_BOOL light_isWritable(char const * filename)
186 186
 
187 187
   if(!fileHandle)
188 188
   {
189
+    LIGHT_PERMWARN("writing");
189 190
     return FALSE;
190 191
   }
191 192
 
@@ -199,6 +200,7 @@ LIGHT_BOOL light_isReadable(char const * filename)
199 200
 
200 201
   if(!fileHandle)
201 202
   {
203
+    LIGHT_PERMWARN("reading");
202 204
     return FALSE;
203 205
   }
204 206
 

+ 16
- 11
src/light.c Wyświetl plik

@@ -23,6 +23,7 @@ void light_defaultConfig()
23 23
 LIGHT_BOOL light_parseArguments(int argc, char** argv)
24 24
 {
25 25
   int currFlag;
26
+  int verbosity;
26 27
 
27 28
   LIGHT_BOOL opSet = FALSE;
28 29
   LIGHT_BOOL targetSet = FALSE;
@@ -133,18 +134,19 @@ LIGHT_BOOL light_parseArguments(int argc, char** argv)
133 134
           light_printHelp();
134 135
           return FALSE;
135 136
         }
136
-        if(sscanf(optarg, "%i", &light_verbosity) != 1)
137
+        if(sscanf(optarg, "%i", &verbosity) != 1)
137 138
         {
138 139
           printf("-v Verbosity is not specified in a recognizable format.\n\n");
139 140
           light_printHelp();
140 141
           return FALSE;
141 142
         }
142
-        if(light_verbosity < 0 || light_verbosity > 3)
143
+        if(verbosity < 0 || verbosity > 3)
143 144
         {
144 145
           printf("-v Verbosity has to be between 0 and 3.\n\n");
145 146
           light_printHelp();
146 147
           return FALSE;
147 148
         }
149
+        light_verbosity = (LIGHT_LOG_LEVEL)verbosity;
148 150
         break;
149 151
     }
150 152
   }
@@ -283,11 +285,11 @@ LIGHT_BOOL light_initialize(int argc, char** argv)
283 285
   }
284 286
   else if(!light_controllerAccessible(light_Configuration.specifiedController))
285 287
   {
286
-    LIGHT_ERR("selected controller is not valid, make sure this application is run as root.");
288
+    LIGHT_ERR_FMT("selected controller '%s' is not valid",
289
+                  light_Configuration.specifiedController);
287 290
     return FALSE;
288 291
   }
289 292
 
290
-
291 293
   return TRUE;
292 294
 }
293 295
 
@@ -334,6 +336,7 @@ LIGHT_BOOL light_execute()
334 336
   }
335 337
 
336 338
   /* Prepare variables */
339
+  LIGHT_NOTE_FMT("Executing light on '%s' controller", light_Configuration.specifiedController);
337 340
 
338 341
   /* -- First, get the current, min and max values directly from controller/configuration (raw values) */
339 342
   if(!light_getBrightness(light_Configuration.specifiedController, &rawCurr))
@@ -356,7 +359,8 @@ LIGHT_BOOL light_execute()
356 359
 
357 360
   if( hasMinCap && minCap > rawMax )
358 361
   {
359
-    LIGHT_WARN("invalid minimum cap for controller, ignoring and using 0");
362
+    LIGHT_WARN_FMT("invalid minimum cap (raw) value of '%lu' for controller, ignoring and using 0", minCap);
363
+    LIGHT_WARN_FMT("minimum cap must be inferior to '%lu'", rawMax);
360 364
     minCap = 0;
361 365
   }
362 366
 
@@ -428,7 +432,7 @@ LIGHT_BOOL light_execute()
428 432
       /* If we are not attempting to set, fail! */
429 433
       if(light_Configuration.operationMode != LIGHT_SET)
430 434
       {
431
-        printf("Minimum cap can only be used with get/set operations.\n");
435
+        fprintf(stderr, "Minimum cap can only be used with get/set operations.\n");
432 436
         return FALSE;
433 437
       }
434 438
 
@@ -483,7 +487,7 @@ LIGHT_BOOL light_execute()
483 487
 
484 488
     }else{
485 489
       /* If we didn't provide a valid target for write operations, fail. */
486
-      printf("set/add/subtract operations are only available for brightness and minimum cap files.\n");
490
+      fprintf(stderr, "set/add/subtract operations are only available for brightness and minimum cap files.\n");
487 491
       return FALSE;
488 492
     }
489 493
   }
@@ -508,9 +512,9 @@ LIGHT_BOOL light_execute()
508 512
     return TRUE;
509 513
   }
510 514
 
511
-  printf("Controller: %s\nValueRaw: %lu\nValuePercent: %.2f\nOpMode: %u\nValMode: %u\nTarget: %u\n\n", light_Configuration.specifiedController, light_Configuration.specifiedValueRaw, light_Configuration.specifiedValuePercent, light_Configuration.operationMode, light_Configuration.valueMode, light_Configuration.target);
515
+  fprintf(stderr, "Controller: %s\nValueRaw: %lu\nValuePercent: %.2f\nOpMode: %u\nValMode: %u\nTarget: %u\n\n", light_Configuration.specifiedController, light_Configuration.specifiedValueRaw, light_Configuration.specifiedValuePercent, light_Configuration.operationMode, light_Configuration.valueMode, light_Configuration.target);
512 516
 
513
-  printf("You did not specify a valid combination of commandline arguments. Have some help: \n");
517
+  fprintf(stderr, "You did not specify a valid combination of commandline arguments. Have some help: \n");
514 518
   light_printHelp();
515 519
   return FALSE;
516 520
 }
@@ -599,7 +603,7 @@ LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v)
599 603
   {
600 604
     return FALSE;
601 605
   }
602
-  LIGHT_NOTE(brightnessPath)
606
+
603 607
   readVal = light_readULong( brightnessPath , v);
604 608
   free(brightnessPath);
605 609
 
@@ -744,7 +748,7 @@ LIGHT_BOOL light_iterateControllers()
744 748
     }
745 749
     if(light_iteratorDir == NULL)
746 750
     {
747
-      LIGHT_ERR("could not open backlight or leds directory");
751
+      LIGHT_ERR("could not open backlight or leds directory in /sys/class");
748 752
       return FALSE;
749 753
     }
750 754
   }
@@ -785,6 +789,7 @@ LIGHT_BOOL light_getBestController(char *controller)
785 789
   {
786 790
     unsigned long currVal = 0;
787 791
 
792
+    LIGHT_NOTE_FMT("found '%s' controller", light_currentController);
788 793
     if(light_controllerAccessible(light_currentController))
789 794
     {
790 795