| 1 |
|
| #define PERL_EXT_XS_LOG 1 |
| 2 |
|
| #include <EXTERN.h> |
| 3 |
|
| #include <perl.h> |
| 4 |
|
| #include <XSUB.h> |
| 5 |
|
| #include <fcntl.h> |
| 6 |
|
| #include <stdarg.h> |
| 7 |
|
| #include <stdio.h> |
| 8 |
|
| #include <string.h> |
| 9 |
|
| #include <sys/file.h> |
| 10 |
|
| #include <sys/types.h> |
| 11 |
|
| #include <time.h> |
| 12 |
|
| #include <unistd.h> |
| 13 |
|
| #include "logger.h" |
| 14 |
|
| |
| 15 |
|
| char* |
| 16 |
71 |
| get_default_file_path() { |
| 17 |
|
| char *path; |
| 18 |
71 |
| SV *sv = get_sv( "XS::Logger::PATH_FILE", 0 ); |
| 19 |
|
| |
| 20 |
71 |
▶ | if ( sv && SvPOK(sv) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( sv && SvPOK(sv) ) |
71 |
0 |
71 |
| if ( sv && SvPOK(sv) ) |
71 |
0 |
71 |
|
| 21 |
71 |
| path = SvPV_nolen( sv ); |
| 22 |
|
| else |
| 23 |
0 |
| path = (char *) DEFAULT_LOG_FILE; /* fallback to default path */ |
| 24 |
|
| |
| 25 |
71 |
| return path; |
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
|
| char* |
| 29 |
68 |
| _file_path_for_logger(MyLogger *self) { |
| 30 |
68 |
▶ | if ( strlen(self->filepath) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( strlen(self->filepath) ) |
34 |
34 |
68 |
|
| 31 |
34 |
| return self->filepath; |
| 32 |
|
| else |
| 33 |
34 |
| return get_default_file_path(); |
| 34 |
|
| } |
| 35 |
|
| |
| 36 |
|
| /* c internal functions */ |
| 37 |
|
| void |
| 38 |
100 |
| do_log(MyLogger *mylogger, logLevel level, const char *fmt, int num_args, ...) { |
| 39 |
100 |
| FILE *fhandle = NULL; |
| 40 |
100 |
| char *path = NULL; |
| 41 |
100 |
| SV *sv = NULL; |
| 42 |
|
| /* Get current time */ |
| 43 |
100 |
| time_t t = time(NULL); |
| 44 |
100 |
| struct tm lt = {0}; |
| 45 |
|
| char buf[32]; |
| 46 |
100 |
| bool has_logger_object = true; |
| 47 |
100 |
| bool hold_lock = false; |
| 48 |
|
| pid_t pid; |
| 49 |
100 |
| bool quiet = false; /* do not display messages on stderr when quiet mode enabled */ |
| 50 |
|
| |
| 51 |
100 |
| localtime_r(&t, <); |
| 52 |
|
| |
| 53 |
100 |
▶ | if ( level == LOG_DISABLE ) /* to move earlier */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( level == LOG_DISABLE ) /* to move earlier */ |
0 |
100 |
100 |
|
| 54 |
0 |
| return; |
| 55 |
|
| |
| 56 |
100 |
| buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", <)] = '\0'; |
| 57 |
|
| |
| 58 |
100 |
| pid = getpid(); |
| 59 |
|
| |
| 60 |
|
| /* Note: *mylogger can be a NULL pointer => would fall back to a GV string or a constant from .c to get the filename */ |
| 61 |
100 |
▶ | if ( mylogger ) { /* we got a mylogger pointer */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( mylogger ) { /* we got a mylogger pointer */ |
63 |
37 |
100 |
|
| 62 |
63 |
| path = _file_path_for_logger( mylogger ); |
| 63 |
|
| |
| 64 |
63 |
▶ | if ( mylogger->pid && mylogger->pid != pid ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( mylogger->pid && mylogger->pid != pid ) { |
50 |
13 |
63 |
| if ( mylogger->pid && mylogger->pid != pid ) { |
1 |
49 |
50 |
|
| 65 |
1 |
▶ | if (mylogger->fhandle) fclose(mylogger->fhandle); |
Branch: 1 branch
| Branch | True | False | Total |
| if (mylogger->fhandle) fclose(mylogger->fhandle); |
1 |
0 |
1 |
|
| 66 |
1 |
| mylogger->fhandle = NULL; |
| 67 |
|
| } |
| 68 |
63 |
▶ | if ( ! mylogger->fhandle ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! mylogger->fhandle ) { |
14 |
49 |
63 |
|
| 69 |
14 |
▶ | if ( (fhandle = fopen( path, "a" )) == NULL ) /* open in append mode */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (fhandle = fopen( path, "a" )) == NULL ) /* open in append mode */ |
0 |
14 |
14 |
|
| 70 |
0 |
| croak("Failed to open file \"%s\"", path); |
| 71 |
14 |
| mylogger->fhandle = fhandle; /* save the fhandle for future reuse */ |
| 72 |
14 |
| mylogger->pid = pid; /* store the pid which open the file */ |
| 73 |
|
| |
| 74 |
14 |
▶ | ACQUIRE_LOCK_ONCE(fhandle); /* get a lock before moving to the end */ |
Branch: 1 branch
| Branch | True | False | Total |
| ACQUIRE_LOCK_ONCE(fhandle); /* get a lock before moving to the end */ |
14 |
0 |
14 |
|
| 75 |
14 |
| fseek(fhandle, 0, SEEK_END); |
| 76 |
|
| } |
| 77 |
63 |
| fhandle = mylogger->fhandle; |
| 78 |
63 |
| quiet = mylogger->quiet; |
| 79 |
|
| } else { |
| 80 |
37 |
| path = get_default_file_path(); |
| 81 |
37 |
| has_logger_object = false; |
| 82 |
|
| |
| 83 |
37 |
▶ | if ( (fhandle = fopen( path, "a" )) == NULL ) /* open in append mode */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (fhandle = fopen( path, "a" )) == NULL ) /* open in append mode */ |
0 |
37 |
37 |
|
| 84 |
0 |
| croak("Failed to open file \"%s\"", path); |
| 85 |
|
| |
| 86 |
37 |
▶ | ACQUIRE_LOCK_ONCE(fhandle); /* get a lock before moving to the end */ |
Branch: 1 branch
| Branch | True | False | Total |
| ACQUIRE_LOCK_ONCE(fhandle); /* get a lock before moving to the end */ |
37 |
0 |
37 |
|
| 87 |
37 |
| fseek(fhandle, 0, SEEK_END); |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
100 |
▶ | if ( fhandle ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( fhandle ) { |
100 |
0 |
100 |
|
| 91 |
|
| va_list args; |
| 92 |
100 |
| int abs_gmtoff = lt.tm_gmtoff >= 0 ? lt.tm_gmtoff : -1 * lt.tm_gmtoff; |
| 93 |
100 |
▶ | if (num_args) va_start(args, num_args); |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_args) va_start(args, num_args); |
58 |
42 |
100 |
|
| 94 |
|
| |
| 95 |
100 |
▶ | ACQUIRE_LOCK_ONCE(fhandle); |
Branch: 1 branch
| Branch | True | False | Total |
| ACQUIRE_LOCK_ONCE(fhandle); |
49 |
51 |
100 |
|
| 96 |
|
| |
| 97 |
|
| /* write the message */ |
| 98 |
|
| /* header: [timestamp tz] pid LEVEL */ |
| 99 |
100 |
▶ | if ( mylogger && mylogger->use_color ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( mylogger && mylogger->use_color ) { |
63 |
37 |
100 |
| if ( mylogger && mylogger->use_color ) { |
56 |
7 |
63 |
|
| 100 |
56 |
▶ | M_FPRINTF( fhandle, "[%s %s%02d%02d] %s%-5s%s", |
Branch: 3 branches
| Branch | True | False | Total |
| M_FPRINTF( fhandle, "[%s %s%02d%02d] %s%-5s%s", |
56 |
0 |
56 |
| M_FPRINTF( fhandle, "[%s %s%02d%02d] %s%-5s%s", |
3 |
53 |
56 |
| M_FPRINTF( fhandle, "[%s %s%02d%02d] %s%-5s%s", |
3 |
0 |
3 |
|
| 101 |
|
| buf, |
| 102 |
|
| lt.tm_gmtoff >= 0 ? "+" : "-", |
| 103 |
|
| (int) abs_gmtoff / 3600, |
| 104 |
|
| ( abs_gmtoff % 3600) / 60, |
| 105 |
|
| LEVEL_COLORS[level], LOG_LEVEL_NAMES[level], END_COLOR |
| 106 |
|
| ); |
| 107 |
|
| } else { |
| 108 |
44 |
▶ | M_FPRINTF( fhandle, "[%s %s%02d%02d] %-5s", |
Branch: 3 branches
| Branch | True | False | Total |
| M_FPRINTF( fhandle, "[%s %s%02d%02d] %-5s", |
44 |
0 |
44 |
| M_FPRINTF( fhandle, "[%s %s%02d%02d] %-5s", |
37 |
7 |
44 |
| M_FPRINTF( fhandle, "[%s %s%02d%02d] %-5s", |
37 |
0 |
37 |
|
| 109 |
|
| buf, |
| 110 |
|
| lt.tm_gmtoff >= 0 ? "+" : "-", |
| 111 |
|
| (int) abs_gmtoff / 3600, ( abs_gmtoff % 3600) / 60, |
| 112 |
|
| LOG_LEVEL_NAMES[level] |
| 113 |
|
| ); |
| 114 |
|
| } |
| 115 |
|
| { |
| 116 |
100 |
| SV *const dollar_0 = get_sv("0",GV_ADDWARN); /* $0 - application name */ |
| 117 |
|
| char *str_dollar_0; |
| 118 |
|
| |
| 119 |
100 |
▶ | if ( !SvPOK(dollar_0) ) { /* probably a better helper to simply get the PV at all cost */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !SvPOK(dollar_0) ) { /* probably a better helper to simply get the PV at all cost */ |
0 |
100 |
100 |
|
| 120 |
0 |
▶ | if ( SvIOK(dollar_0) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvIOK(dollar_0) ) |
0 |
0 |
0 |
|
| 121 |
0 |
▶ | SvUPGRADE(dollar_0, SVt_PVIV); |
Branch: 1 branch
| Branch | True | False | Total |
| SvUPGRADE(dollar_0, SVt_PVIV); |
0 |
0 |
0 |
|
| 122 |
|
| else |
| 123 |
0 |
| croak("dollar_0 is not a string?!"); |
| 124 |
|
| } |
| 125 |
100 |
| str_dollar_0 = SvPV_nolen( dollar_0 ); |
| 126 |
100 |
▶ | M_FPRINTF( fhandle, " %u [%s] ", (unsigned int) pid, str_dollar_0 ); /* print the source */ |
Branch: 1 branch
| Branch | True | False | Total |
| M_FPRINTF( fhandle, " %u [%s] ", (unsigned int) pid, str_dollar_0 ); /* print the source */ |
40 |
60 |
100 |
|
| 127 |
|
| /* with the pid ? */ |
| 128 |
|
| /* fprintf( fhandle, " [%u %s] ", (unsigned int) pid, str_dollar_0 ); */ |
| 129 |
|
| } |
| 130 |
|
| { |
| 131 |
100 |
| int len = 0; |
| 132 |
|
| |
| 133 |
|
| //PerlIO_printf( PerlIO_stderr(), "# num_args %d\n", num_args ); |
| 134 |
100 |
▶ | if ( fmt && (len=strlen(fmt)) ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( fmt && (len=strlen(fmt)) ) { |
100 |
0 |
100 |
| if ( fmt && (len=strlen(fmt)) ) { |
58 |
42 |
100 |
|
| 135 |
58 |
▶ | if (num_args == 0) /* no need to use sprintf when not needed */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_args == 0) /* no need to use sprintf when not needed */ |
0 |
58 |
58 |
|
| 136 |
0 |
▶ | M_FPUTS( fmt, fhandle ) |
Branch: 1 branch
| Branch | True | False | Total |
| M_FPUTS( fmt, fhandle ) |
0 |
0 |
0 |
|
| 137 |
|
| else |
| 138 |
58 |
▶ | M_VFPRINTF( fhandle, fmt, args ) |
Branch: 2 branches
| Branch | True | False | Total |
| M_VFPRINTF( fhandle, fmt, args ) |
25 |
33 |
58 |
| M_VFPRINTF( fhandle, fmt, args ) |
25 |
33 |
58 |
|
| 139 |
|
| } |
| 140 |
|
| // only add "\n" if missing from fmt |
| 141 |
100 |
▶ | if ( !len || fmt[len-1] != '\n') |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !len || fmt[len-1] != '\n') |
58 |
42 |
100 |
| if ( !len || fmt[len-1] != '\n') |
57 |
1 |
58 |
|
| 142 |
99 |
▶ | M_FPUTS( "\n", fhandle ); |
Branch: 1 branch
| Branch | True | False | Total |
| M_FPUTS( "\n", fhandle ); |
40 |
59 |
99 |
|
| 143 |
|
| } |
| 144 |
100 |
▶ | if (has_logger_object) fflush(fhandle); /* otherwise we are going to close the ffhandle just after */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (has_logger_object) fflush(fhandle); /* otherwise we are going to close the ffhandle just after */ |
63 |
37 |
100 |
|
| 145 |
100 |
▶ | if (num_args) va_end(args); |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_args) va_end(args); |
58 |
42 |
100 |
|
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
100 |
▶ | RELEASE_LOCK(fhandle); /* only release if acquired before */ |
Branch: 1 branch
| Branch | True | False | Total |
| RELEASE_LOCK(fhandle); /* only release if acquired before */ |
100 |
0 |
100 |
|
| 149 |
|
| |
| 150 |
100 |
▶ | if ( !has_logger_object ) fclose( fhandle ); |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !has_logger_object ) fclose( fhandle ); |
37 |
63 |
100 |
|
| 151 |
|
| |
| 152 |
100 |
| return; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| /* function exposed to the module */ |
| 156 |
|
| /* maybe a bad idea to use a prefix */ |
| 157 |
|
| MODULE = XS__Logger PACKAGE = XS::Logger PREFIX = xlog_ |
| 158 |
|
| |
| 159 |
|
| TYPEMAP: <<HERE |
| 160 |
|
| |
| 161 |
|
| MyLogger* T_PTROBJ |
| 162 |
|
| XS::Logger T_PTROBJ |
| 163 |
|
| |
| 164 |
|
| HERE |
| 165 |
|
| |
| 166 |
|
| XS::Logger |
| 167 |
|
| xlog_new(class, ...) |
| 168 |
|
| char* class; |
| 169 |
|
| PREINIT: |
| 170 |
|
| MyLogger* mylogger; |
| 171 |
21 |
| HV* opts = NULL; |
| 172 |
|
| SV **svp; |
| 173 |
|
| CODE: |
| 174 |
|
| { |
| 175 |
21 |
| Newxz( mylogger, 1, MyLogger ); |
| 176 |
21 |
| RETVAL = mylogger; |
| 177 |
|
| |
| 178 |
21 |
▶ | if( items > 1 ) { /* could also probably use va_start, va_list, ... */ |
Branch: 1 branch
| Branch | True | False | Total |
| if( items > 1 ) { /* could also probably use va_start, va_list, ... */ |
16 |
5 |
21 |
|
| 179 |
16 |
| SV *extra = (SV*) ST(1); |
| 180 |
|
| |
| 181 |
16 |
▶ | if ( SvROK(extra) && SvTYPE(SvRV(extra)) == SVt_PVHV ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( SvROK(extra) && SvTYPE(SvRV(extra)) == SVt_PVHV ) |
16 |
0 |
16 |
| if ( SvROK(extra) && SvTYPE(SvRV(extra)) == SVt_PVHV ) |
16 |
0 |
16 |
|
| 182 |
16 |
| opts = (HV*) SvRV( extra ); |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
|
| /* default (non zero) values */ |
| 186 |
21 |
| mylogger->use_color = true; /* maybe use a GV from the stash to set the default value */ |
| 187 |
21 |
▶ | if ( opts ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( opts ) { |
16 |
5 |
21 |
|
| 188 |
16 |
▶ | if ( (svp = hv_fetchs(opts, "color", FALSE)) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (svp = hv_fetchs(opts, "color", FALSE)) ) { |
3 |
13 |
16 |
|
| 189 |
3 |
▶ | if (!SvIOK(*svp)) croak("invalid color option value: should be a boolean 1/0"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvIOK(*svp)) croak("invalid color option value: should be a boolean 1/0"); |
0 |
3 |
3 |
|
| 190 |
3 |
| mylogger->use_color = (bool) SvIV(*svp); |
| 191 |
|
| } |
| 192 |
16 |
▶ | if ( (svp = hv_fetchs(opts, "level", FALSE)) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (svp = hv_fetchs(opts, "level", FALSE)) ) { |
6 |
10 |
16 |
|
| 193 |
6 |
▶ | if (!SvIOK(*svp)) croak("invalid log level: should be one integer"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvIOK(*svp)) croak("invalid log level: should be one integer"); |
0 |
6 |
6 |
|
| 194 |
6 |
| mylogger->level = (logLevel) SvIV(*svp); |
| 195 |
|
| } |
| 196 |
16 |
▶ | if ( (svp = hv_fetchs(opts, "quiet", FALSE)) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (svp = hv_fetchs(opts, "quiet", FALSE)) ) { |
13 |
3 |
16 |
|
| 197 |
13 |
▶ | if (!SvIOK(*svp)) croak("invalid quiet value: should be one integer 0 or 1"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvIOK(*svp)) croak("invalid quiet value: should be one integer 0 or 1"); |
0 |
13 |
13 |
|
| 198 |
13 |
| mylogger->quiet = (logLevel) SvIV(*svp); |
| 199 |
|
| } |
| 200 |
16 |
▶ | if ( (svp = hv_fetchs(opts, "logfile", FALSE)) || (svp = hv_fetchs(opts, "path", FALSE)) ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( (svp = hv_fetchs(opts, "logfile", FALSE)) || (svp = hv_fetchs(opts, "path", FALSE)) ) { |
13 |
3 |
16 |
| if ( (svp = hv_fetchs(opts, "logfile", FALSE)) || (svp = hv_fetchs(opts, "path", FALSE)) ) { |
3 |
10 |
13 |
|
| 201 |
|
| STRLEN len; |
| 202 |
|
| char *src; |
| 203 |
|
| |
| 204 |
6 |
▶ | if (!SvPOK(*svp)) croak("invalid logfile path: must be a string"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvPOK(*svp)) croak("invalid logfile path: must be a string"); |
0 |
6 |
6 |
|
| 205 |
6 |
| src = SvPV(*svp, len); |
| 206 |
6 |
▶ | if (len >= sizeof(mylogger->filepath)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= sizeof(mylogger->filepath)) |
0 |
6 |
6 |
|
| 207 |
0 |
| croak("file path too long max=256!"); |
| 208 |
6 |
| strcpy(mylogger->filepath, src); /* do a copy to the object */ |
| 209 |
|
| } |
| 210 |
|
| } |
| 211 |
|
| } |
| 212 |
|
| OUTPUT: |
| 213 |
|
| RETVAL |
| 214 |
|
| |
| 215 |
|
| void |
| 216 |
|
| xlog_loggers(...) |
| 217 |
|
| ALIAS: |
| 218 |
|
| XS::Logger::info = 1 |
| 219 |
|
| XS::Logger::warn = 2 |
| 220 |
|
| XS::Logger::error = 3 |
| 221 |
|
| XS::Logger::die = 4 |
| 222 |
|
| XS::Logger::panic = 5 |
| 223 |
|
| XS::Logger::fatal = 6 |
| 224 |
|
| XS::Logger::debug = 7 |
| 225 |
|
| PREINIT: |
| 226 |
|
| SV *ret; |
| 227 |
|
| SV* self; /* optional */ |
| 228 |
|
| CODE: |
| 229 |
|
| { |
| 230 |
115 |
| logLevel level = LOG_DISABLE; |
| 231 |
115 |
| bool dolog = true; |
| 232 |
115 |
| MyLogger* mylogger = NULL; /* can be null when not called on an object */ |
| 233 |
115 |
| int args_start_at = 0; |
| 234 |
115 |
| bool should_die = false; |
| 235 |
|
| const char *fmt; |
| 236 |
115 |
| MultiValue targs[10] = {0}; /* no need to malloc limited to 10 */ |
| 237 |
|
| |
| 238 |
115 |
| switch (ix) { |
| 239 |
38 |
| case 1: /* info */ |
| 240 |
38 |
| level = LOG_INFO; |
| 241 |
38 |
| break; |
| 242 |
13 |
| case 2: /* warn */ |
| 243 |
13 |
| level = LOG_WARN; |
| 244 |
13 |
| break; |
| 245 |
12 |
| case 3: /* error */ |
| 246 |
12 |
| level = LOG_ERROR; |
| 247 |
12 |
| break; |
| 248 |
6 |
| case 4: /* die */ |
| 249 |
6 |
| level = LOG_ERROR; |
| 250 |
6 |
| should_die = true; |
| 251 |
6 |
| break; |
| 252 |
18 |
| case 5: /* panic */ |
| 253 |
|
| case 6: /* fatal */ |
| 254 |
18 |
| level = LOG_FATAL; |
| 255 |
18 |
| should_die = true; |
| 256 |
18 |
| break; |
| 257 |
28 |
| case 7: |
| 258 |
28 |
| level = LOG_DEBUG; |
| 259 |
28 |
| break; |
| 260 |
0 |
| default: |
| 261 |
0 |
| level = LOG_DISABLE; |
| 262 |
|
| } |
| 263 |
|
| |
| 264 |
|
| /* check if called as function or method call */ |
| 265 |
115 |
▶ | if ( items && SvROK(ST(0)) && SvOBJECT(SvRV(ST(0))) ) { /* check if self is an object */ |
Branch: 3 branches
| Branch | True | False | Total |
| if ( items && SvROK(ST(0)) && SvOBJECT(SvRV(ST(0))) ) { /* check if self is an object */ |
100 |
15 |
115 |
| if ( items && SvROK(ST(0)) && SvOBJECT(SvRV(ST(0))) ) { /* check if self is an object */ |
78 |
22 |
100 |
| if ( items && SvROK(ST(0)) && SvOBJECT(SvRV(ST(0))) ) { /* check if self is an object */ |
78 |
0 |
78 |
|
| 266 |
78 |
| self = ST(0); |
| 267 |
78 |
| args_start_at = 1; |
| 268 |
78 |
| mylogger = INT2PTR(MyLogger*, SvIV(SvRV(self))); |
| 269 |
|
| /* check the caller level */ |
| 270 |
78 |
▶ | if ( level < mylogger->level ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( level < mylogger->level ) |
10 |
68 |
78 |
|
| 271 |
10 |
| dolog = false; |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
115 |
▶ | if (dolog) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dolog) { |
105 |
10 |
115 |
|
| 275 |
|
| SV **list; |
| 276 |
105 |
▶ | if ( items < (1 + args_start_at) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( items < (1 + args_start_at) ) { |
42 |
63 |
105 |
|
| 277 |
42 |
| fmt = EMPTY_STR; |
| 278 |
42 |
| do_log( mylogger, level, fmt, 0 ); /* do a simple call */ |
| 279 |
63 |
▶ | } else if ( items <= ( 11 + args_start_at ) ) { /* set a cap on the maximum of item we can use: 10 arguments + 1 format + 1 for self */ |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( items <= ( 11 + args_start_at ) ) { /* set a cap on the maximum of item we can use: 10 arguments + 1 format + 1 for self */ |
58 |
5 |
63 |
|
| 280 |
|
| IV i; |
| 281 |
58 |
| I32 nitems = items - args_start_at; /* for self */ |
| 282 |
|
| |
| 283 |
|
| //Newx(list, nitems, SV*); |
| 284 |
183 |
▶ | for ( i = args_start_at ; i < items ; ++i ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = args_start_at ; i < items ; ++i ) { |
125 |
58 |
183 |
|
| 285 |
125 |
| SV *sv = ST(i); |
| 286 |
125 |
▶ | if ( !SvOK(sv) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !SvOK(sv) ) |
0 |
125 |
125 |
|
| 287 |
0 |
| croak( "Invalid element item %i - not an SV.", (int) i ); |
| 288 |
|
| else { |
| 289 |
|
| /* do a switch on the type */ |
| 290 |
125 |
▶ | if ( i == args_start_at ) { /* the first entry shoulkd be the format */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( i == args_start_at ) { /* the first entry shoulkd be the format */ |
58 |
67 |
125 |
|
| 291 |
58 |
▶ | if ( !SvPOK(sv) ) { /* maybe upgrade to a PV */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !SvPOK(sv) ) { /* maybe upgrade to a PV */ |
1 |
57 |
58 |
|
| 292 |
1 |
▶ | if ( SvIOK(sv) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvIOK(sv) ) |
1 |
0 |
1 |
|
| 293 |
1 |
▶ | SvUPGRADE(sv, SVt_PVIV); |
Branch: 1 branch
| Branch | True | False | Total |
| SvUPGRADE(sv, SVt_PVIV); |
1 |
0 |
1 |
|
| 294 |
|
| else |
| 295 |
0 |
| croak("First argument must be a string."); |
| 296 |
|
| } |
| 297 |
58 |
| fmt = SvPV_nolen( sv ); |
| 298 |
|
| } else { |
| 299 |
67 |
| int ix = i - 1 - args_start_at; |
| 300 |
|
| |
| 301 |
67 |
▶ | if ( SvIOK(sv) ) { /* SvTYPE(sv) == SVt_IV */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvIOK(sv) ) { /* SvTYPE(sv) == SVt_IV */ |
61 |
6 |
67 |
|
| 302 |
61 |
| targs[ix].ival = SvIV(sv); |
| 303 |
6 |
▶ | } else if ( SvNOK(sv) ) { // not working for now |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( SvNOK(sv) ) { // not working for now |
1 |
5 |
6 |
|
| 304 |
|
| //PerlIO_printf( PerlIO_stderr(), "# SV SV %f\n", 1.345 ); |
| 305 |
|
| //PerlIO_printf( PerlIO_stderr(), "# SV SV %f\n", SvNV(sv) ); |
| 306 |
1 |
| targs[ix].fval = SvNV(sv); |
| 307 |
|
| } else { |
| 308 |
5 |
| targs[ix].sval = SvPV_nolen(sv); |
| 309 |
|
| } |
| 310 |
|
| } |
| 311 |
|
| } |
| 312 |
|
| } |
| 313 |
|
| /* not really necessary but probaby better for performance */ |
| 314 |
58 |
| switch ( nitems ) { |
| 315 |
42 |
| case 1: |
| 316 |
42 |
| do_log( mylogger, level, fmt, nitems, |
| 317 |
|
| targs[0] |
| 318 |
|
| ); |
| 319 |
42 |
| break; |
| 320 |
4 |
| case 2: |
| 321 |
4 |
| do_log( mylogger, level, fmt, nitems, |
| 322 |
|
| targs[0], targs[1] |
| 323 |
|
| ); |
| 324 |
4 |
| break; |
| 325 |
2 |
| case 3: |
| 326 |
2 |
| do_log( mylogger, level, fmt, nitems, |
| 327 |
|
| targs[0], targs[1], targs[2] |
| 328 |
|
| ); |
| 329 |
2 |
| break; |
| 330 |
2 |
| case 4: |
| 331 |
2 |
| do_log( mylogger, level, fmt, nitems, |
| 332 |
|
| targs[0], targs[1], targs[2], targs[3] |
| 333 |
|
| ); |
| 334 |
2 |
| break; |
| 335 |
2 |
| case 5: |
| 336 |
2 |
| do_log( mylogger, level, fmt, nitems, |
| 337 |
|
| targs[0], targs[1], targs[2], targs[3], targs[4] |
| 338 |
|
| ); |
| 339 |
2 |
| break; |
| 340 |
1 |
| case 6: |
| 341 |
1 |
| do_log( mylogger, level, fmt, nitems, |
| 342 |
|
| targs[0], targs[1], targs[2], targs[3], targs[4], |
| 343 |
|
| targs[5] |
| 344 |
|
| ); |
| 345 |
1 |
| break; |
| 346 |
1 |
| case 7: |
| 347 |
1 |
| do_log( mylogger, level, fmt, nitems, |
| 348 |
|
| targs[0], targs[1], targs[2], targs[3], targs[4], |
| 349 |
|
| targs[5], targs[6] |
| 350 |
|
| ); |
| 351 |
1 |
| break; |
| 352 |
1 |
| case 8: |
| 353 |
1 |
| do_log( mylogger, level, fmt, nitems, |
| 354 |
|
| targs[0], targs[1], targs[2], targs[3], targs[4], |
| 355 |
|
| targs[5], targs[6], targs[7] |
| 356 |
|
| ); |
| 357 |
1 |
| break; |
| 358 |
1 |
| case 9: |
| 359 |
1 |
| do_log( mylogger, level, fmt, nitems, |
| 360 |
|
| targs[0], targs[1], targs[2], targs[3], targs[4], |
| 361 |
|
| targs[5], targs[6], targs[7], targs[8] |
| 362 |
|
| ); |
| 363 |
1 |
| break; |
| 364 |
1 |
| case 10: |
| 365 |
1 |
| do_log( mylogger, level, fmt, nitems, |
| 366 |
|
| targs[0], targs[1], targs[2], targs[3], targs[4], |
| 367 |
|
| targs[5], targs[6], targs[7], targs[8], targs[9] |
| 368 |
|
| ); |
| 369 |
1 |
| break; |
| 370 |
1 |
| default: |
| 371 |
1 |
| do_log( mylogger, level, fmt, nitems, |
| 372 |
|
| targs[0], targs[1], targs[2], targs[3], targs[4], |
| 373 |
|
| targs[5], targs[6], targs[7], targs[8], targs[9] |
| 374 |
|
| ); |
| 375 |
|
| } |
| 376 |
|
| } else { |
| 377 |
5 |
| croak("Too many args to the caller (max=10)."); |
| 378 |
|
| } |
| 379 |
|
| } /* end of dolog */ |
| 380 |
|
| |
| 381 |
110 |
▶ | if ( should_die ) /* maybe fatal needs to exit */ { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( should_die ) /* maybe fatal needs to exit */ { |
24 |
86 |
110 |
|
| 382 |
|
| /* FIXME: right now only using the fmt without the args */ |
| 383 |
|
| /* exit level [panic] [pid=6904] (This is a message) */ |
| 384 |
24 |
| croak( "exit level [%s] [pid=%d] (%s)\n", LOG_LEVEL_NAMES_lc[level], getpid(), |
| 385 |
|
| fmt |
| 386 |
|
| ); |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
|
| /* no need to return anything there */ |
| 390 |
86 |
| XSRETURN_EMPTY; |
| 391 |
|
| } |
| 392 |
|
| |
| 393 |
|
| SV* |
| 394 |
|
| xlog_getters(self) |
| 395 |
|
| XS::Logger self; |
| 396 |
|
| ALIAS: |
| 397 |
|
| XS::Logger::get_pid = 1 |
| 398 |
|
| XS::Logger::use_color = 2 |
| 399 |
|
| XS::Logger::get_level = 3 |
| 400 |
|
| XS::Logger::get_quiet = 4 |
| 401 |
|
| XS::Logger::get_file_path = 5 |
| 402 |
|
| XS::Logger::logfile = 5 |
| 403 |
|
| PREINIT: |
| 404 |
|
| MyLogger* mylogger; |
| 405 |
|
| char *fp; |
| 406 |
|
| CODE: |
| 407 |
|
| { /* some getters: mainly used for test for now to access internals */ |
| 408 |
|
| |
| 409 |
14 |
| switch (ix) { |
| 410 |
1 |
| case 1: |
| 411 |
1 |
| RETVAL = newSViv( self->pid ); |
| 412 |
1 |
| break; |
| 413 |
2 |
| case 2: |
| 414 |
2 |
| RETVAL = newSViv( self->use_color ); |
| 415 |
2 |
| break; |
| 416 |
3 |
| case 3: |
| 417 |
3 |
| RETVAL = newSViv( (int) self->level ); |
| 418 |
3 |
| break; |
| 419 |
3 |
| case 4: |
| 420 |
3 |
| RETVAL = newSViv( (int) self->quiet ); |
| 421 |
3 |
| break; |
| 422 |
5 |
| case 5: |
| 423 |
5 |
| fp = _file_path_for_logger( self ); |
| 424 |
5 |
| RETVAL = newSVpv( fp, strlen(fp) ); |
| 425 |
5 |
| break; |
| 426 |
0 |
| default: |
| 427 |
0 |
| XSRETURN_EMPTY; |
| 428 |
|
| } |
| 429 |
|
| } |
| 430 |
|
| OUTPUT: |
| 431 |
|
| RETVAL |
| 432 |
|
| |
| 433 |
|
| void |
| 434 |
|
| xlog_setters(self, value) |
| 435 |
|
| XS::Logger self; |
| 436 |
|
| SV* value; |
| 437 |
|
| ALIAS: |
| 438 |
|
| XS::Logger::set_level = 1 |
| 439 |
|
| XS::Logger::set_quiet = 2 |
| 440 |
|
| PREINIT: |
| 441 |
|
| MyLogger* mylogger; |
| 442 |
|
| CODE: |
| 443 |
|
| { /* improve protection on self/logger here */ |
| 444 |
|
| |
| 445 |
2 |
| switch (ix) { |
| 446 |
1 |
| case 1: |
| 447 |
1 |
▶ | if ( !SvIOK(value) ) croak("invalid level: must be interger."); |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !SvIOK(value) ) croak("invalid level: must be interger."); |
0 |
1 |
1 |
|
| 448 |
1 |
| self->level = SvIV(value); |
| 449 |
1 |
| break; |
| 450 |
1 |
| case 2: |
| 451 |
1 |
▶ | if ( !SvIOK(value) ) croak("invalid quiet value: must be interger."); |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !SvIOK(value) ) croak("invalid quiet value: must be interger."); |
0 |
1 |
1 |
|
| 452 |
1 |
| self->quiet = SvIV(value); |
| 453 |
1 |
| break; |
| 454 |
0 |
| default: |
| 455 |
0 |
| croak("undefined setter"); |
| 456 |
|
| } |
| 457 |
|
| |
| 458 |
2 |
| XSRETURN_EMPTY; |
| 459 |
|
| } |
| 460 |
|
| |
| 461 |
|
| void xlog_DESTROY(self) |
| 462 |
|
| XS::Logger self; |
| 463 |
|
| PREINIT: |
| 464 |
|
| I32* temp; |
| 465 |
|
| PPCODE: |
| 466 |
|
| { |
| 467 |
21 |
| temp = PL_markstack_ptr++; |
| 468 |
|
| |
| 469 |
21 |
▶ | if ( self ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( self ) { |
21 |
0 |
21 |
|
| 470 |
|
| /* close the file fhandle on destroy if exists */ |
| 471 |
21 |
▶ | if ( self->fhandle ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( self->fhandle ) |
13 |
8 |
21 |
|
| 472 |
13 |
| fclose( self->fhandle ); |
| 473 |
|
| /* free the logger... maybe more to clear from struct */ |
| 474 |
21 |
| Safefree(self); |
| 475 |
|
| } |
| 476 |
|
| |
| 477 |
21 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
21 |
0 |
21 |
|
| 478 |
|
| /* truly void, because dXSARGS not invoked */ |
| 479 |
21 |
| PL_markstack_ptr = temp; |
| 480 |
21 |
| XSRETURN_EMPTY; |
| 481 |
|
| /* return empty stack */ |
| 482 |
|
| } /* must have used dXSARGS; list context implied */ |
| 483 |
|
| |
| 484 |
0 |
| return; /* assume stack size is correct */ |
| 485 |
|
| } |
| 486 |
|
| |
| 487 |
|
| BOOT: |
| 488 |
|
| { |
| 489 |
|
| HV *stash; |
| 490 |
|
| SV *sv; |
| 491 |
9 |
| stash = gv_stashpvn("XS::Logger", 10, TRUE); |
| 492 |
|
| |
| 493 |
9 |
| newCONSTSUB(stash, "_loaded", newSViv(1) ); |
| 494 |
9 |
| newCONSTSUB(stash, "DEBUG_LOG_LEVEL", newSViv( LOG_DEBUG ) ); |
| 495 |
9 |
| newCONSTSUB(stash, "INFO_LOG_LEVEL", newSViv( LOG_INFO ) ); |
| 496 |
9 |
| newCONSTSUB(stash, "WARN_LOG_LEVEL", newSViv( LOG_WARN ) ); |
| 497 |
9 |
| newCONSTSUB(stash, "ERROR_LOG_LEVEL", newSViv( LOG_ERROR ) ); |
| 498 |
9 |
| newCONSTSUB(stash, "FATAL_LOG_LEVEL", newSViv( LOG_FATAL ) ); |
| 499 |
9 |
| newCONSTSUB(stash, "DISABLE_LOG_LEVEL", newSViv( LOG_DISABLE ) ); |
| 500 |
|
| |
| 501 |
9 |
| sv = get_sv("XS::Logger::PATH_FILE", GV_ADD|GV_ADDMULTI); |
| 502 |
9 |
▶ | if ( ! SvPOK(sv) ) { /* preserve any value set before loading the module */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! SvPOK(sv) ) { /* preserve any value set before loading the module */ |
8 |
1 |
9 |
|
| 503 |
8 |
| SvREFCNT_inc(sv); |
| 504 |
8 |
| sv_setpv(sv, DEFAULT_LOG_FILE); |
| 505 |
|
| } |
| 506 |
|
| } |