Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_common.c
1
7#include <stdio.h>
8#include <errno.h>
9#include <string.h>
10#include <sys/types.h>
11
12#include "nilorea/n_common.h"
13#include "nilorea/n_log.h"
14#include "nilorea/n_str.h"
15
16#ifndef __windows__
17#include <sys/wait.h>
18#endif
19
20void usage(void) {
21 fprintf(stderr,
22 " -v version\n"
23 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
24 " -h help\n");
25}
26
27void process_args(int argc, char** argv) {
28 int getoptret = 0,
29 log_level = LOG_DEBUG; /* default log level */
30
31 /* Arguments optionnels */
32 /* -v version
33 * -V log level
34 * -h help
35 */
36 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
37 switch (getoptret) {
38 case 'v':
39 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
40 exit(1);
41 case 'V':
42 if (!strncmp("LOG_NULL", optarg, 5)) {
43 log_level = LOG_NULL;
44 } else {
45 if (!strncmp("LOG_NOTICE", optarg, 6)) {
46 log_level = LOG_NOTICE;
47 } else {
48 if (!strncmp("LOG_INFO", optarg, 7)) {
49 log_level = LOG_INFO;
50 } else {
51 if (!strncmp("LOG_ERR", optarg, 5)) {
52 log_level = LOG_ERR;
53 } else {
54 if (!strncmp("LOG_DEBUG", optarg, 5)) {
55 log_level = LOG_DEBUG;
56 } else {
57 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
58 exit(-1);
59 }
60 }
61 }
62 }
63 }
64 break;
65 default:
66 case '?': {
67 if (optopt == 'V') {
68 fprintf(stderr, "\n Missing log level\n");
69 } else if (optopt == 'p') {
70 fprintf(stderr, "\n Missing port\n");
71 } else if (optopt != 's') {
72 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
73 }
74 usage();
75 exit(1);
76 }
77 case 'h': {
78 usage();
79 exit(1);
80 }
81 }
82 }
83 set_log_level(log_level);
84} /* void process_args( ... ) */
85
86FORCE_INLINE void inlined_func(void) {
87 n_log(LOG_DEBUG, "Inlined func");
88}
89
90int main(int argc, char** argv) {
91 /* processing args and set log_level */
92 process_args(argc, argv);
93
94 inlined_func();
95
96 n_log(LOG_INFO, "TRUE,true values: %d,%d / FALSE,false value: %d,%d", TRUE, true, FALSE, false);
97
98 n_log(LOG_INFO, "_str(\"TEST\")=\"%s\" , _str(NULL)=\"%s\"", _str("TEST"), _str(NULL));
99 n_log(LOG_INFO, "_strw(\"TEST\")=\"%s\" , _strw(NULL)=\"%s\"", _str("TEST"), _str(NULL));
100 N_STR* nstr = char_to_nstr("TEST");
101 n_log(LOG_INFO, "_nstr(nstrpointer)=\"%s\"", _nstr(nstr));
102 n_log(LOG_INFO, "_nstrp(nstrpointer)=\"%s\"", _nstrp(nstr));
103 free_nstr(&nstr);
104 n_log(LOG_INFO, "_nstr(nstrpointer_freed)=\"%s\"", _nstr(nstr));
105 n_log(LOG_INFO, "_nstrp(nstrpointer_freed)=\"%s\"", _nstrp(nstr));
106
107 char* data = NULL;
108 __n_assert(data, n_log(LOG_INFO, "data is NULL"));
109 Malloc(data, char, 1024);
110 __n_assert(data, n_log(LOG_INFO, "data is NULL"));
111 Free(data);
112 Malloc(data, char, 1024);
113 Realloc(data, char, 2048);
114 Free(data);
115 Malloc(data, char, 1024);
116 Reallocz(data, char, 1024, 2048);
117 FreeNoLog(data);
118 Alloca(data, 2048);
119 // Free( data ); alloca should not be free else it's double freeing on exit
120
121 n_log(LOG_INFO, "next_odd(10):%d , next_odd(11):%d", next_odd(10), next_odd(11));
122 n_log(LOG_INFO, "next_even(10):%d , next_even(11):%d", next_even(10), next_even(11));
123
125 ifzero(0) endif;
126 ifzero(1) endif;
127 ifnull(data) endif;
128 ifnull(NULL) endif;
129 iffalse(FALSE) endif;
130 iftrue(TRUE) endif;
131 checkerror();
132error:
133 if (get_error()) {
134 n_log(LOG_INFO, "got an error while processing test");
135 } else {
136 n_log(LOG_INFO, "All tests for checkerror() are OK");
137 }
138
139 if (file_exist(argv[0])) {
140 n_log(LOG_INFO, "%s exists !", argv[0]);
141 }
142
143 char *dir = NULL, *name = NULL;
144 dir = get_prog_dir();
145 name = get_prog_name();
146 n_log(LOG_INFO, "From %s/%s", dir, name);
147 Free(dir);
148 Free(name);
149
150 N_STR* out = NULL;
151 int ret = -1;
152 if (n_popen("ls -ltr", 2048, (void*)&out, &ret) == TRUE) {
153 n_log(LOG_INFO, "ls returned %d : %s", ret, _nstr(out));
154 } else {
155 n_log(LOG_ERR, "popen s returned an error");
156 }
157 free_nstr(&out);
158
159 char hidden_str[47] = "";
160 N_HIDE_STR(hidden_str, 'T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'w', 'i', 'l', 'l', ' ', 'b', 'e', ' ', 'h', 'i', 'd', 'd', 'e', 'n', ' ', 'i', 'n', ' ', 't', 'h', 'e', ' ', 'f', 'i', 'n', 'a', 'l', ' ', 'b', 'i', 'n', 'a', 'r', 'y', '\0');
161 printf("hidden str:%s\n", hidden_str);
162
163#ifndef __windows__
164 n_log(LOG_INFO, "before system_nb( sleep 3 )");
165 int pid = system_nb("sleep 3", NULL, NULL);
166 n_log(LOG_INFO, "after system_nb( sleep 3 )");
167 n_log(LOG_INFO, "wait for nb sys call");
168 wait(&pid);
169 n_log(LOG_INFO, "done");
170 n_daemonize();
171#endif
172 n_abort("Testing abort before exit");
173 n_log(LOG_INFO, "abort done");
174 exit(0);
175}
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:251
char * get_prog_name(void)
get current program name
Definition n_common.c:127
#define get_error()
pop up errors if any
Definition n_common.h:312
void N_HIDE_STR(char *buf,...)
store a hidden version of a string
Definition n_common.c:429
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:187
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:258
#define _str(__PTR)
define true
Definition n_common.h:176
#define ifzero
error checker type if( 0 != toto )
Definition n_common.h:290
pid_t system_nb(const char *command, int *infp, int *outfp)
Non blocking system call.
Definition n_common.c:361
#define iffalse
error checker type if( toto == FALSE )
Definition n_common.h:293
#define Reallocz(__ptr, __struct, __old_size, __size)
Realloc + zero new memory zone Handler to get errors.
Definition n_common.h:227
#define next_odd(__val)
next odd helper
Definition n_common.h:277
#define Alloca(__ptr, __size)
Malloca Handler to get errors and set to 0.
Definition n_common.h:199
#define endif
close a ifwhatever block
Definition n_common.h:306
#define ifnull
error checker type if( !toto )
Definition n_common.h:287
#define _nstrp(__PTR)
N_STR or NULL pointer for testing purposes.
Definition n_common.h:184
#define next_even(__val)
next odd helper
Definition n_common.h:280
char * get_prog_dir(void)
get current program running directory
Definition n_common.c:95
#define FORCE_INLINE
FORCE_INLINE portable macro.
Definition n_common.h:147
#define iftrue
error checker type if( toto == FALSE )
Definition n_common.h:296
#define checkerror()
check for errors
Definition n_common.h:299
void n_abort(char const *format,...)
abort program with a text
Definition n_common.c:38
int n_daemonize(void)
Daemonize program.
Definition n_common.c:254
#define init_error_check()
init error checking in a function
Definition n_common.h:283
#define Realloc(__ptr, __struct, __size)
Realloc Handler to get errors.
Definition n_common.h:213
int file_exist(const char *filename)
test if file exist and if it's readable
Definition n_common.c:82
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:242
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:182
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_DEBUG
debug-level messages
Definition n_log.h:64
#define LOG_ERR
error conditions
Definition n_log.h:56
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:91
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:60
#define LOG_NULL
no log output
Definition n_log.h:26
#define LOG_INFO
informational
Definition n_log.h:62
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:228
A box including a string and his lenght.
Definition n_str.h:39
Common headers and low-level functions & define.
Generic log system.
N_STR and string function declaration.