init
This commit is contained in:
commit
866225c8e4
|
@ -0,0 +1,25 @@
|
|||
BSD 2-Clause License
|
||||
|
||||
Copyright (c) 2018, Muresan Vlad Mihail
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@ -0,0 +1,21 @@
|
|||
```c
|
||||
#include "logger.h"
|
||||
|
||||
int main() {
|
||||
log_info("Starting!");
|
||||
log_warning("This is a warning!");
|
||||
log_error("This shoudn't have happened!");
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
OUTPUT:
|
||||
|
||||
Tue Nov 6 13:18:11 2018
|
||||
INFO main.c:4: Starting!
|
||||
|
||||
Tue Nov 6 13:18:11 2018
|
||||
WARNING main.c:5: This is a warning!
|
||||
|
||||
Tue Nov 6 13:18:11 2018
|
||||
ERROR main.c:6: This shoudn't have happened!
|
|
@ -0,0 +1,8 @@
|
|||
#include "logger.h"
|
||||
|
||||
int main() {
|
||||
log_info("Starting! %s", "123\n");
|
||||
log_warning("This is a warning!");
|
||||
log_error("This shoudn't have happened!");
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
#include "logger.h"
|
||||
|
||||
void _logger(int level, char *file, int line, char* format, ...) {
|
||||
|
||||
time_t t = time(NULL);
|
||||
|
||||
switch (level) {
|
||||
case LOG_LEVEL_ERROR:
|
||||
fprintf(stderr, "%sERROR %s:%d: ", asctime(localtime(&t)), file, line);
|
||||
break;
|
||||
case LOG_LEVEL_WARNING:
|
||||
fprintf(stderr, "%sWARNING %s:%d: ", asctime(localtime(&t)), file, line);
|
||||
break;
|
||||
case LOG_LEVEL_INFO:
|
||||
fprintf(stdout, "%sINFO %s:%d: ", asctime(localtime(&t)), file, line);
|
||||
break;
|
||||
case LOG_LEVEL_DEBUG:
|
||||
fprintf(stdout, "%sDEBUG %s:%d: ", asctime(localtime(&t)), file, line);
|
||||
break;
|
||||
}
|
||||
va_list args;
|
||||
|
||||
va_start(args, format);
|
||||
vprintf(format, args);
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef _LOGGER_H_
|
||||
#define _LOGGER_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define log_error(...) _logger(LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_warning(...) _logger(LOG_LEVEL_WARNING, __FILE__, __LINE__,__VA_ARGS__)
|
||||
#define log_info(...) _logger(LOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_debug(...) _logger(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
|
||||
|
||||
enum {
|
||||
LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_WARNING,
|
||||
LOG_LEVEL_INFO,
|
||||
LOG_LEVEL_DEBUG
|
||||
} LOG_LEVEL;
|
||||
|
||||
void _logger(int level, char *file, int line, char *format, ...);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue