Nilorea Library
C utilities for networking, threading, graphics
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{
22 fprintf( stderr, " -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{
29 int getoptret = 0,
30 log_level = LOG_DEBUG ; /* default log level */
31
32 /* Arguments optionnels */
33 /* -v version
34 * -V log level
35 * -h help
36 */
37 while( ( getoptret = getopt( argc, argv, "hvV:" ) ) != EOF)
38 {
39 switch( getoptret )
40 {
41 case 'v' :
42 fprintf( stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__ );
43 exit( 1 );
44 case 'V' :
45 if( !strncmp( "LOG_NULL", optarg, 5 ) )
46 {
47 log_level = LOG_NULL ;
48 }
49 else
50 {
51 if( !strncmp( "LOG_NOTICE", optarg, 6 ) )
52 {
53 log_level = LOG_NOTICE;
54 }
55 else
56 {
57 if( !strncmp( "LOG_INFO", optarg, 7 ) )
58 {
59 log_level = LOG_INFO;
60 }
61 else
62 {
63 if( !strncmp( "LOG_ERR", optarg, 5 ) )
64 {
65 log_level = LOG_ERR;
66 }
67 else
68 {
69 if( !strncmp( "LOG_DEBUG", optarg, 5 ) )
70 {
71 log_level = LOG_DEBUG;
72 }
73 else
74 {
75 fprintf( stderr, "%s n'est pas un niveau de log valide.\n", optarg );
76 exit( -1 );
77 }
78 }
79 }
80 }
81 }
82 break;
83 default :
84 case '?' :
85 {
86 if( optopt == 'V' )
87 {
88 fprintf( stderr, "\n Missing log level\n" );
89 }
90 else if( optopt == 'p' )
91 {
92 fprintf( stderr, "\n Missing port\n" );
93 }
94 else if( optopt != 's' )
95 {
96 fprintf( stderr, "\n Unknow missing option %c\n", optopt );
97 }
98 usage();
99 exit( 1 );
100 }
101 case 'h' :
102 {
103 usage();
104 exit( 1 );
105 }
106 }
107 }
108 set_log_level( log_level );
109} /* void process_args( ... ) */
110
111
112
113FORCE_INLINE void inlined_func( void )
114{
115 n_log( LOG_DEBUG, "Inlined func" );
116}
117
118
119int main(int argc, char **argv)
120{
121
122 /* processing args and set log_level */
123 process_args( argc, argv );
124
125 inlined_func();
126
127 n_log( LOG_INFO, "TRUE,true values: %d,%d / FALSE,false value: %d,%d", TRUE, true, FALSE, false );
128
129
130 n_log( LOG_INFO, "_str(\"TEST\")=\"%s\" , _str(NULL)=\"%s\"", _str( "TEST" ), _str( NULL ) );
131 n_log( LOG_INFO, "_strw(\"TEST\")=\"%s\" , _strw(NULL)=\"%s\"", _str( "TEST" ), _str( NULL ) );
132 N_STR *nstr = char_to_nstr( "TEST" );
133 n_log( LOG_INFO, "_nstr(nstrpointer)=\"%s\"", _nstr( nstr ) );
134 n_log( LOG_INFO, "_nstrp(nstrpointer)=\"%s\"", _nstrp( nstr ) );
135 free_nstr( &nstr );
136 n_log( LOG_INFO, "_nstr(nstrpointer_freed)=\"%s\"", _nstr( nstr ) );
137 n_log( LOG_INFO, "_nstrp(nstrpointer_freed)=\"%s\"", _nstrp( nstr ) );
138
139 char *data = NULL ;
140 __n_assert( data, n_log( LOG_INFO, "data is NULL" ) );
141 Malloc( data, char, 1024 );
142 __n_assert( data, n_log( LOG_INFO, "data is NULL" ) );
143 Free( data );
144 Malloc( data, char, 1024 );
145 Realloc( data, char, 2048 );
146 Free( data );
147 Malloc( data, char, 1024 );
148 Reallocz( data, char, 1024, 2048 );
149 FreeNoLog( data );
150 Alloca( data, 2048 );
151 //Free( data ); alloca should not be free else it's double freeing on exit
152
153 n_log( LOG_INFO, "next_odd(10):%d , next_odd(11):%d", next_odd( 10 ), next_odd( 11 ) );
154 n_log( LOG_INFO, "next_even(10):%d , next_even(11):%d", next_even( 10 ), next_even( 11 ) );
155
157 ifzero (0) endif ;
158 ifzero (1) endif ;
159 ifnull (data) endif ;
160 ifnull (NULL) endif ;
161 iffalse (FALSE) endif ;
162 iftrue (TRUE) endif ;
163 checkerror();
164error:
165 if( get_error() )
166 {
167 n_log( LOG_INFO , "got an error while processing test" );
168 }
169 else
170 {
171 n_log( LOG_INFO , "All tests for checkerror() are OK" );
172 }
173
174 if( file_exist( argv[ 0 ] ) )
175 {
176 n_log( LOG_INFO, "%s exists !", argv[ 0 ] );
177 }
178
179 char *dir = NULL, *name = NULL ;
180 dir = get_prog_dir();
181 name = get_prog_name();
182 n_log( LOG_INFO, "From %s/%s", dir, name );
183 Free( dir );
184 Free( name );
185
186 N_STR *out = NULL ;
187 int ret = -1 ;
188 if( n_popen( "ls -ltr", 2048, (void *)&out, &ret ) == TRUE )
189 {
190 n_log( LOG_INFO, "ls returned %d : %s", ret, _nstr( out ) );
191 }
192 else
193 {
194 n_log( LOG_ERR, "popen s returned an error" );
195 }
196 free_nstr( &out );
197
198 char hidden_str[47]="";
199 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');
200 printf("hidden str:%s\n" , hidden_str);
201
202#ifndef __windows__
203 n_log( LOG_INFO, "before system_nb( sleep 3 )" );
204 int pid=system_nb( "sleep 3", NULL, NULL );
205 n_log( LOG_INFO, "after system_nb( sleep 3 )" );
206 n_log( LOG_INFO, "wait for nb sys call" );
207 wait( &pid );
208 n_log( LOG_INFO, "done" );
209 n_daemonize();
210#endif
211 n_abort( "Testing abort before exit" );
212 n_log( LOG_INFO, "abort done" );
213 exit( 0 );
214}
#define FreeNoLog(__ptr)
Free Handler without log.
Definition: n_common.h:268
char * get_prog_name(void)
get current program name
Definition: n_common.c:145
#define get_error()
pop up errors if any
Definition: n_common.h:321
void N_HIDE_STR(char *buf,...)
store a hidden version of a string
Definition: n_common.c:371
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition: n_common.h:183
#define __n_assert(__ptr, __ret)
macro to assert things
Definition: n_common.h:276
#define _str(__PTR)
define true
Definition: n_common.h:172
#define ifzero
error checker type if( 0 != toto )
Definition: n_common.h:303
pid_t system_nb(const char *command, int *infp, int *outfp)
Non blocking system call.
Definition: n_common.c:293
#define iffalse
error checker type if( toto == FALSE )
Definition: n_common.h:306
#define Reallocz(__ptr, __struct, __old_size, __size)
Realloc + zero new memory zone Handler to get errors.
Definition: n_common.h:236
#define next_odd(__val)
next odd helper
Definition: n_common.h:289
#define Alloca(__ptr, __size)
Malloca Handler to get errors and set to 0.
Definition: n_common.h:198
int n_popen(char *cmd, int read_buf_size, void **nstr_output, int *ret)
launch a command abd return output and status
Definition: n_common.c:185
#define endif
close a ifwhatever block
Definition: n_common.h:318
#define ifnull
error checker type if( !toto )
Definition: n_common.h:300
#define _nstrp(__PTR)
N_STR or NULL pointer for testing purposes.
Definition: n_common.h:180
#define next_even(__val)
next odd helper
Definition: n_common.h:292
char * get_prog_dir(void)
get current program running directory
Definition: n_common.c:109
#define FORCE_INLINE
FORCE_INLINE portable macro.
Definition: n_common.h:141
#define iftrue
error checker type if( toto == FALSE )
Definition: n_common.h:309
#define checkerror()
check for errors
Definition: n_common.h:312
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:236
#define init_error_check()
init error checking in a function
Definition: n_common.h:296
#define Realloc(__ptr, __struct, __size)
Realloc Handler to get errors.
Definition: n_common.h:217
int file_exist(const char *filename)
test if file exist and if it's readable
Definition: n_common.c:92
#define Free(__ptr)
Free Handler to get errors.
Definition: n_common.h:256
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition: n_common.h:178
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_DEBUG
debug-level messages
Definition: n_log.h:66
#define LOG_ERR
error conditions
Definition: n_log.h:58
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition: n_log.c:97
#define LOG_NOTICE
normal but significant condition
Definition: n_log.h:62
#define LOG_NULL
no log output
Definition: n_log.h:27
#define LOG_INFO
informational
Definition: n_log.h:64
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition: n_str.h:222
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition: n_str.c:273
A box including a string and his lenght.
Definition: n_str.h:173
Common headers and low-level hugly functions & define.
Generic log system.
N_STR and string function declaration.