1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6 
7 #include "global.h"
8 
9 #ifdef _WIN32
10 
11 #define RED   ""
12 #define BOLD  ""
13 #define RESET ""
14 
15 #else
16 
17 #define RED "\x1b[31;1m"
18 #define BOLD "\x1b[;1m"
19 #define RESET "\x1b[0m"
20 
21 #endif
22 
23 enum {
24   LOG_LEVEL_NORMAL,
25   LOG_LEVEL_VERBOSE
26 };
27 
28 int LOG_LEVEL;
29 
printWarn(const char * fmt,...)30 void printWarn(const char *fmt, ...) {
31   if(LOG_LEVEL == LOG_LEVEL_VERBOSE) {
32     int buffer_size = 4096;
33     char buffer[buffer_size];
34     va_list args;
35     va_start(args, fmt);
36     vsnprintf(buffer,buffer_size, fmt, args);
37     va_end(args);
38     fprintf(stderr,BOLD "[WARNING]: "RESET "%s\n",buffer);
39   }
40 }
41 
printErr(const char * fmt,...)42 void printErr(const char *fmt, ...) {
43   int buffer_size = 4096;
44   char buffer[buffer_size];
45   va_list args;
46   va_start(args, fmt);
47   vsnprintf(buffer,buffer_size, fmt, args);
48   va_end(args);
49   fprintf(stderr,RED "[ERROR]: "RESET "%s\n",buffer);
50 }
51 
printBug(const char * fmt,...)52 void printBug(const char *fmt, ...) {
53   int buffer_size = 4096;
54   char buffer[buffer_size];
55   va_list args;
56   va_start(args, fmt);
57   vsnprintf(buffer,buffer_size, fmt, args);
58   va_end(args);
59   fprintf(stderr,RED "[ERROR]: "RESET "%s\n",buffer);
60 #if defined(ARCH_X86) || defined(ARCH_PPC)
61   fprintf(stderr,"Please, create a new issue with this error message and the output of 'cpufetch --debug' on https://github.com/Dr-Noob/cpufetch/issues\n");
62 #elif ARCH_ARM
63   fprintf(stderr,"Please, create a new issue with this error message, your smartphone/computer model and the output of 'cpufetch --debug' on https://github.com/Dr-Noob/cpufetch/issues\n");
64 #endif
65 }
66 
set_log_level(bool verbose)67 void set_log_level(bool verbose) {
68   if(verbose) LOG_LEVEL = LOG_LEVEL_VERBOSE;
69   else LOG_LEVEL = LOG_LEVEL_NORMAL;
70 }
71 
max(int a,int b)72 int max(int a, int b) {
73   return a > b ? a : b;
74 }
75 
min(int a,int b)76 int min(int a, int b) {
77   return a < b ? a : b;
78 }
79 
strremove(char * str,const char * sub)80 char *strremove(char *str, const char *sub) {
81   char *p, *q, *r;
82   if (*sub && (q = r = strstr(str, sub)) != NULL) {
83     size_t len = strlen(sub);
84     while ((r = strstr(p = r + len, sub)) != NULL) {
85       memmove(q, p, r - p);
86       q += r - p;
87     }
88     memmove(q, p, strlen(p) + 1);
89   }
90   return str;
91 }
92 
emalloc(size_t size)93 void* emalloc(size_t size) {
94   void* ptr = malloc(size);
95 
96   if(ptr == NULL) {
97     printErr("malloc failed: %s", strerror(errno));
98     exit(1);
99   }
100 
101   return ptr;
102 }
103 
ecalloc(size_t nmemb,size_t size)104 void* ecalloc(size_t nmemb, size_t size) {
105   void* ptr = calloc(nmemb, size);
106 
107   if(ptr == NULL) {
108     printErr("calloc failed: %s", strerror(errno));
109     exit(1);
110   }
111 
112   return ptr;
113 }
114