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