Abdullah ibn Nadjo пре 6 година
родитељ
комит
ccff2b57cd
4 измењених фајлова са 59 додато и 28 уклоњено
  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 Прегледај датотеку

3
 MANDIR=$(PREFIX)/share/man/man1
3
 MANDIR=$(PREFIX)/share/man/man1
4
 
4
 
5
 CC=gcc
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
 MANFLAGS=-h -h -v -V -N
7
 MANFLAGS=-h -h -v -V -N
8
 
8
 
9
 HELP2MAN_VERSION := $(shell help2man --version 2>/dev/null)
9
 HELP2MAN_VERSION := $(shell help2man --version 2>/dev/null)

+ 33
- 9
include/helpers.h Прегледај датотеку

15
  * }*/
15
  * }*/
16
 #define LIGHT_CLAMP(x, y, z) ((x<y) ? y : ((x>z) ? z : x ));
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
 /* Verbosity levels: 
19
 /* Verbosity levels: 
27
  * 0 - No output
20
  * 0 - No output
28
  * 1 - Errors
21
  * 1 - Errors
29
  * 2 - Errors, warnings 
22
  * 2 - Errors, warnings 
30
  * 3 - Errors, warnings, notices */
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
 /* Typedef for boolean values */
57
 /* Typedef for boolean values */
34
 typedef enum LIGHT_BOOL {
58
 typedef enum LIGHT_BOOL {

+ 9
- 7
src/helpers.c Прегледај датотеку

15
 
15
 
16
   if(!fileHandle)
16
   if(!fileHandle)
17
   {
17
   {
18
-    LIGHT_ERR("could not open file for reading");
18
+    LIGHT_PERMERR("reading");
19
     return FALSE;
19
     return FALSE;
20
   }
20
   }
21
 
21
 
22
   if(fscanf(fileHandle, "%u", &iCopy) != 1)
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
     fclose(fileHandle);
25
     fclose(fileHandle);
26
     return FALSE;
26
     return FALSE;
27
   }
27
   }
40
 
40
 
41
   if(!fileHandle)
41
   if(!fileHandle)
42
   {
42
   {
43
-    LIGHT_ERR("could not open file for writing");
43
+    LIGHT_PERMERR("writing");
44
     return FALSE;
44
     return FALSE;
45
   }
45
   }
46
 
46
 
65
 
65
 
66
   if(!fileHandle)
66
   if(!fileHandle)
67
   {
67
   {
68
-    LIGHT_ERR("could not open file for reading");
68
+    LIGHT_PERMERR("reading");
69
     return FALSE;
69
     return FALSE;
70
   }
70
   }
71
 
71
 
72
   if(fscanf(fileHandle, "%lu", &iCopy) != 1)
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
     fclose(fileHandle);
75
     fclose(fileHandle);
76
     return FALSE;
76
     return FALSE;
77
   }
77
   }
90
 
90
 
91
   if(!fileHandle)
91
   if(!fileHandle)
92
   {
92
   {
93
-    LIGHT_ERR("could not open file for writing");
93
+    LIGHT_PERMERR("writing");
94
     return FALSE;
94
     return FALSE;
95
   }
95
   }
96
 
96
 
125
 
125
 
126
   if(!fileHandle)
126
   if(!fileHandle)
127
   {
127
   {
128
-    LIGHT_ERR("could not open file for reading");
128
+    LIGHT_PERMERR("reading");
129
     return FALSE;
129
     return FALSE;
130
   }
130
   }
131
 
131
 
186
 
186
 
187
   if(!fileHandle)
187
   if(!fileHandle)
188
   {
188
   {
189
+    LIGHT_PERMWARN("writing");
189
     return FALSE;
190
     return FALSE;
190
   }
191
   }
191
 
192
 
199
 
200
 
200
   if(!fileHandle)
201
   if(!fileHandle)
201
   {
202
   {
203
+    LIGHT_PERMWARN("reading");
202
     return FALSE;
204
     return FALSE;
203
   }
205
   }
204
 
206
 

+ 16
- 11
src/light.c Прегледај датотеку

23
 LIGHT_BOOL light_parseArguments(int argc, char** argv)
23
 LIGHT_BOOL light_parseArguments(int argc, char** argv)
24
 {
24
 {
25
   int currFlag;
25
   int currFlag;
26
+  int verbosity;
26
 
27
 
27
   LIGHT_BOOL opSet = FALSE;
28
   LIGHT_BOOL opSet = FALSE;
28
   LIGHT_BOOL targetSet = FALSE;
29
   LIGHT_BOOL targetSet = FALSE;
133
           light_printHelp();
134
           light_printHelp();
134
           return FALSE;
135
           return FALSE;
135
         }
136
         }
136
-        if(sscanf(optarg, "%i", &light_verbosity) != 1)
137
+        if(sscanf(optarg, "%i", &verbosity) != 1)
137
         {
138
         {
138
           printf("-v Verbosity is not specified in a recognizable format.\n\n");
139
           printf("-v Verbosity is not specified in a recognizable format.\n\n");
139
           light_printHelp();
140
           light_printHelp();
140
           return FALSE;
141
           return FALSE;
141
         }
142
         }
142
-        if(light_verbosity < 0 || light_verbosity > 3)
143
+        if(verbosity < 0 || verbosity > 3)
143
         {
144
         {
144
           printf("-v Verbosity has to be between 0 and 3.\n\n");
145
           printf("-v Verbosity has to be between 0 and 3.\n\n");
145
           light_printHelp();
146
           light_printHelp();
146
           return FALSE;
147
           return FALSE;
147
         }
148
         }
149
+        light_verbosity = (LIGHT_LOG_LEVEL)verbosity;
148
         break;
150
         break;
149
     }
151
     }
150
   }
152
   }
283
   }
285
   }
284
   else if(!light_controllerAccessible(light_Configuration.specifiedController))
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
     return FALSE;
290
     return FALSE;
288
   }
291
   }
289
 
292
 
290
-
291
   return TRUE;
293
   return TRUE;
292
 }
294
 }
293
 
295
 
334
   }
336
   }
335
 
337
 
336
   /* Prepare variables */
338
   /* Prepare variables */
339
+  LIGHT_NOTE_FMT("Executing light on '%s' controller", light_Configuration.specifiedController);
337
 
340
 
338
   /* -- First, get the current, min and max values directly from controller/configuration (raw values) */
341
   /* -- First, get the current, min and max values directly from controller/configuration (raw values) */
339
   if(!light_getBrightness(light_Configuration.specifiedController, &rawCurr))
342
   if(!light_getBrightness(light_Configuration.specifiedController, &rawCurr))
356
 
359
 
357
   if( hasMinCap && minCap > rawMax )
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
     minCap = 0;
364
     minCap = 0;
361
   }
365
   }
362
 
366
 
428
       /* If we are not attempting to set, fail! */
432
       /* If we are not attempting to set, fail! */
429
       if(light_Configuration.operationMode != LIGHT_SET)
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
         return FALSE;
436
         return FALSE;
433
       }
437
       }
434
 
438
 
483
 
487
 
484
     }else{
488
     }else{
485
       /* If we didn't provide a valid target for write operations, fail. */
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
       return FALSE;
491
       return FALSE;
488
     }
492
     }
489
   }
493
   }
508
     return TRUE;
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
   light_printHelp();
518
   light_printHelp();
515
   return FALSE;
519
   return FALSE;
516
 }
520
 }
599
   {
603
   {
600
     return FALSE;
604
     return FALSE;
601
   }
605
   }
602
-  LIGHT_NOTE(brightnessPath)
606
+
603
   readVal = light_readULong( brightnessPath , v);
607
   readVal = light_readULong( brightnessPath , v);
604
   free(brightnessPath);
608
   free(brightnessPath);
605
 
609
 
744
     }
748
     }
745
     if(light_iteratorDir == NULL)
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
       return FALSE;
752
       return FALSE;
749
     }
753
     }
750
   }
754
   }
785
   {
789
   {
786
     unsigned long currVal = 0;
790
     unsigned long currVal = 0;
787
 
791
 
792
+    LIGHT_NOTE_FMT("found '%s' controller", light_currentController);
788
     if(light_controllerAccessible(light_currentController))
793
     if(light_controllerAccessible(light_currentController))
789
     {
794
     {
790
 
795