Compare commits

...

3 Commits

Author SHA1 Message Date
Ilja Kartašov 35c8b96967 Add string module 2019-06-19 21:04:31 +02:00
Ilja Kartašov c85c6cc819 Add log module 2019-06-19 21:04:14 +02:00
Ilja Kartašov d72aebcd09 Add retcode.h 2019-06-19 21:03:58 +02:00
5 changed files with 353 additions and 0 deletions

103
log.c Normal file
View File

@ -0,0 +1,103 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file log.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief cStuff log module source file
*
* @see https://lowenware.com/
*/
#include <time.h>
#include "log.h"
void
cstuff_log_init(CStuffLog self, uint8_t level)
{
self->target = stderr;
self->level = level;
}
void
cstuff_log_release(CStuffLog self)
{
if (self->target != stderr)
fclose(self->target);
}
CStuffRetcode
cstuff_log_set_file(CStuffLog self, const char *filename)
{
FILE *fp;
if (!(fp = fopen(filename, "a")))
return CSTUFF_SYSCALL_ERROR;
self->target = fp;
return CSTUFF_SUCCESS;
}
int
cstuff_log_printf(CStuffLog self, int level, const char *format, ...)
{
int result;
va_list vl;
va_start(vl, format);
result = cstuff_log_vprintf(self, level, format, vl);
va_end(vl);
return result;
}
int
cstuff_log_vprintf(CStuffLog self, int level, const char *format, va_list vl)
{
int result;
if (self->level & level) {
FILE *f = self->target;
char *label;
time_t rawtime;
char strtime[32+1];
struct tm timeinfo;
time(&rawtime);
localtime_r(&rawtime, &timeinfo);
strftime(strtime, 32, "%F %T", &timeinfo);
switch (level)
{
case CSTUFF_LOG_ERROR:
label = "!!";
break;
case CSTUFF_LOG_ALERT:
label = "~!";
break;
case CSTUFF_LOG_DEBUG:
label = "**";
break;
default:
label = "--";
}
result = fprintf(f, "%s %s ", strtime, label);
result += vfprintf(f, format, vl);
fputc('\n', f);
result++;
fflush(f);
} else {
result = 0;
}
return result;
}

58
log.h Normal file
View File

@ -0,0 +1,58 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file log.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief cStuff log module header file
*
* @see https://lowenware.com/cStuff/
*/
#ifndef CSTUFF_LOG_H_557ADEFE_2BE8_4B9B_A894_F08FF64008EF
#define CSTUFF_LOG_H_557ADEFE_2BE8_4B9B_A894_F08FF64008EF
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include "retcode.h"
#define CSTUFF_LOG_OFF (0)
#define CSTUFF_LOG_ERROR (1<<0)
#define CSTUFF_LOG_ALERT (1<<1)
#define CSTUFF_LOG_STATE (1<<2)
#define CSTUFF_LOG_DEBUG (1<<3)
#define CSTUFF_LOG_ALL (0xFF)
struct cstuff_log {
FILE *target;
uint8_t level;
};
typedef struct cstuff_log * CStuffLog;
void
cstuff_log_init(CStuffLog self, int level);
void
cstuff_log_release(CStuffLog self);
CStuffRetcode
cstuff_log_set_file(CStuffLog self, const char *filename);
int
cstuff_log_printf(CStuffLog self, int level, const char *format, ...);
int
cstuff_log_vprintf(CStuffLog self, int level, const char *format, va_list vl);
#endif /* !CSTUFF_LOG_H */

27
retcode.h Normal file
View File

@ -0,0 +1,27 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file retcode.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief cStuff return codes
*
* @see https://lowenware.com/
*/
#ifndef CSTUFF_RETCODE_H_CBA3C16B_88D6_49F4_966D_DBDA727583FE
#define CSTUFF_RETCODE_H_CBA3C16B_88D6_49F4_966D_DBDA727583FE
typedef enum {
CSTUFF_MALLOC_ERROR = -2
, CSTUFF_SYSCALL_ERROR = -1
, CSTUFF_SUCCESS = 0
, CSTUFF_IDLE = 1
} CStuffRetcode;
#endif /* !CSTUFF_RETCODE_H */

104
string.c Normal file
View File

@ -0,0 +1,104 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file string.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief cStuff string module implementation
*
* @see https://lowenware.com/
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "string.h"
int
cstuff_strcpy(char **out, const char *in)
{
return cstuff_strncpy(out, in, strlen(in));
}
int
cstuff_strncpy(char **out, const char *in, int len)
{
char *s;
if (!(s = malloc( len + 1 )))
return -1;
strncpy(s, in, len);
s[len]=0;
*out = s;
return len;
}
int
cstuff_strset(char **out, const char *in)
{
return cstuff_strnset(out, in, strlen(in));
}
int
cstuff_strnset(char **out, const char *in, int len)
{
char *s;
if (!(s = realloc(*out, len+1)))
return -1;
strncpy(s, in, len);
s[len] = 0;
*out = s;
return len;
}
int
cstuff_sprintf(char **out, const char *format, ...)
{
int result;
va_list vl;
va_start(vl, format);
result = cstuff_vsprintf(out, format, vl);
va_end(vl);
return result;
}
int
cstuff_vsprintf(char **out, const char *format, va_list args)
{
int result;
va_list vc;
char tmp, *s;
va_copy(vc, args);
result = vsnprintf(&tmp, 1, format, vc);
va_end(vc);
if (!(s = malloc(result + 1)))
return -1;
*out = s;
result = vsnprintf(s, result+1, format, args);
return result;
}

61
string.h Normal file
View File

@ -0,0 +1,61 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file string.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief String module declarations
*
* @see https://lowenware.com/
*/
#ifndef CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87
#define CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87
#include <stdarg.h>
#include "retcode.h"
/** @brief Copies in string to newly allocated out buffer
* @param out a pointer to an address where new pointer must be stored
* @param in an input string
* @return length of string or -1 if out of memory
*/
int
cstuff_strcpy(char **out, const char *in);
int
cstuff_strncpy(char **out, const char *in, int len);
/** @brief Overwrites content of out buffer with in string using realloc
* @param out a pointer to an address where new string must be stored
* @param in an input string
* @return length of string or -1 if out of memory
*/
int
cstuff_strset(char **out, const char *in);
int
cstuff_strnset(char **out, const char *in, int len);
/** @brief Allocates memory and prints a formatted string into it
* @param out a pointer to an address where new string must be stored
* @param format a format for string (see manual for stdlib sprintf)
* @return length of string or -1 if out of memory
*/
int
cstuff_sprintf(char **out, const char *format, ...);
int
cstuff_vsprintf(char **out, const char *format, va_list args);
#endif /* !CSTUFF_STRING_H */