Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuelchristo committed Apr 30, 2024
2 parents a66d7c7 + ab3ac79 commit cc70104
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 604 deletions.
Binary file added docs/assets/references/logger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
214 changes: 107 additions & 107 deletions docs/guides/references/vec.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,114 +7,9 @@ This library is a copy of the work at https://github.com/rxi/vec and is Copyrigh

## Installation

The vec.c and vec.h files can be dropped into an existing C project and compiled along with it.
The `vec.h` and `vec.c` files can be dropped into an existing C project and compiled along with it.

::: details lib/vec/vec.c

```c
/**
* Copyright (c) 2014 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details.
*/

#include "vec.h"

int vec_expand_(char **data, int *length, int *capacity, int memsz) {
if (*length + 1 > *capacity) {
void *ptr;
int n = (*capacity == 0) ? 1 : *capacity << 1;
ptr = realloc(*data, n * memsz);
if (ptr == NULL)
return -1;
*data = ptr;
*capacity = n;
}
return 0;
}

int vec_reserve_(char **data, int *length, int *capacity, int memsz, int n) {
(void)length;
if (n > *capacity) {
void *ptr = realloc(*data, n * memsz);
if (ptr == NULL)
return -1;
*data = ptr;
*capacity = n;
}
return 0;
}

int vec_reserve_po2_(char **data, int *length, int *capacity, int memsz, int n) {
int n2 = 1;
if (n == 0)
return 0;
while (n2 < n)
n2 <<= 1;
return vec_reserve_(data, length, capacity, memsz, n2);
}

int vec_compact_(char **data, int *length, int *capacity, int memsz) {
if (*length == 0) {
free(*data);
*data = NULL;
*capacity = 0;
return 0;
} else {
void *ptr;
int n = *length;
ptr = realloc(*data, n * memsz);
if (ptr == NULL)
return -1;
*capacity = n;
*data = ptr;
}
return 0;
}

int vec_insert_(char **data, int *length, int *capacity, int memsz, int idx) {
int err = vec_expand_(data, length, capacity, memsz);
if (err)
return err;
memmove(*data + (idx + 1) * memsz, *data + idx * memsz, (*length - idx) * memsz);
return 0;
}

void vec_splice_(char **data, int *length, int *capacity, int memsz, int start, int count) {
(void)capacity;
memmove(*data + start * memsz, *data + (start + count) * memsz,
(*length - start - count) * memsz);
}

void vec_swapsplice_(char **data, int *length, int *capacity, int memsz, int start, int count) {
(void)capacity;
memmove(*data + start * memsz, *data + (*length - count) * memsz, count * memsz);
}

void vec_swap_(char **data, int *length, int *capacity, int memsz, int idx1, int idx2) {
unsigned char *a, *b, tmp;
int count;
(void)length;
(void)capacity;
if (idx1 == idx2)
return;
a = (unsigned char *)*data + idx1 * memsz;
b = (unsigned char *)*data + idx2 * memsz;
count = memsz;
while (count--) {
tmp = *a;
*a = *b;
*b = tmp;
a++, b++;
}
}

```
:::
::: details lib/vec/vec.h
::: details expserver/src/lib/vec/vec.h

```c
/**
Expand Down Expand Up @@ -302,6 +197,111 @@ typedef vec_t(double) vec_double_t;

:::

::: details expserver/src/lib/vec/vec.c

```c
/**
* Copyright (c) 2014 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details.
*/

#include "vec.h"

int vec_expand_(char **data, int *length, int *capacity, int memsz) {
if (*length + 1 > *capacity) {
void *ptr;
int n = (*capacity == 0) ? 1 : *capacity << 1;
ptr = realloc(*data, n * memsz);
if (ptr == NULL)
return -1;
*data = ptr;
*capacity = n;
}
return 0;
}

int vec_reserve_(char **data, int *length, int *capacity, int memsz, int n) {
(void)length;
if (n > *capacity) {
void *ptr = realloc(*data, n * memsz);
if (ptr == NULL)
return -1;
*data = ptr;
*capacity = n;
}
return 0;
}

int vec_reserve_po2_(char **data, int *length, int *capacity, int memsz, int n) {
int n2 = 1;
if (n == 0)
return 0;
while (n2 < n)
n2 <<= 1;
return vec_reserve_(data, length, capacity, memsz, n2);
}

int vec_compact_(char **data, int *length, int *capacity, int memsz) {
if (*length == 0) {
free(*data);
*data = NULL;
*capacity = 0;
return 0;
} else {
void *ptr;
int n = *length;
ptr = realloc(*data, n * memsz);
if (ptr == NULL)
return -1;
*capacity = n;
*data = ptr;
}
return 0;
}

int vec_insert_(char **data, int *length, int *capacity, int memsz, int idx) {
int err = vec_expand_(data, length, capacity, memsz);
if (err)
return err;
memmove(*data + (idx + 1) * memsz, *data + idx * memsz, (*length - idx) * memsz);
return 0;
}

void vec_splice_(char **data, int *length, int *capacity, int memsz, int start, int count) {
(void)capacity;
memmove(*data + start * memsz, *data + (start + count) * memsz,
(*length - start - count) * memsz);
}

void vec_swapsplice_(char **data, int *length, int *capacity, int memsz, int start, int count) {
(void)capacity;
memmove(*data + start * memsz, *data + (*length - count) * memsz, count * memsz);
}

void vec_swap_(char **data, int *length, int *capacity, int memsz, int idx1, int idx2) {
unsigned char *a, *b, tmp;
int count;
(void)length;
(void)capacity;
if (idx1 == idx2)
return;
a = (unsigned char *)*data + idx1 * memsz;
b = (unsigned char *)*data + idx2 * memsz;
count = memsz;
while (count--) {
tmp = *a;
*a = *b;
*b = tmp;
a++, b++;
}
}

```
:::
## Usage
Before using a vector it should first be initialised using the `vec_init()`
Expand Down
150 changes: 150 additions & 0 deletions docs/guides/references/xps_logger.md
Original file line number Diff line number Diff line change
@@ -1 +1,151 @@
# xps_logger

`xps_logger` is a utility designed to provide logging functionality to aid in the development of eXpServer. It includes a `logger()` function that enables logging messages at different levels and supports colored output depending on the error level.

## Source files

The `xps_logger.h` and `xps_logger.c` files can be dropped into your existing C project and compiled along with it.

::: details **expserver/src/utils/xps_logger.h**

```c
#ifndef XPS_LOGGER_H
#define XPS_LOGGER_H

#include "../xps.h"

// Basic text colors
#define BLACK_TEXT "\x1b[30m"
#define RED_TEXT "\x1b[31m"
#define GREEN_TEXT "\x1b[32m"
#define YELLOW_TEXT "\x1b[33m"
#define BLUE_TEXT "\x1b[34m"
#define MAGENTA_TEXT "\x1b[35m"
#define CYAN_TEXT "\x1b[36m"
#define WHITE_TEXT "\x1b[37m"

// Extended text colors (256 colors)
#define EXTENDED_TEXT(x) "\x1b[38;5;" #x "m"

// Basic background colors
#define BLACK_BG "\x1b[40m"
#define RED_BG "\x1b[41m"
#define GREEN_BG "\x1b[42m"
#define YELLOW_BG "\x1b[43m"
#define BLUE_BG "\x1b[44m"
#define MAGENTA_BG "\x1b[45m"
#define CYAN_BG "\x1b[46m"
#define WHITE_BG "\x1b[47m"

// Extended background colors (256 colors)
#define EXTENDED_BG(x) "\x1b[48;5;" #x "m"

#define RESET_COLOR "\x1b[0m"

#define BOLD_START "\033[1m"
#define BOLD_END "\033[0m"

typedef enum { LOG_ERROR, LOG_INFO, LOG_DEBUG, LOG_WARNING, LOG_HTTP } xps_log_level_t;

void logger(xps_log_level_t level, const char *function_name, const char *format_string, ...);

#endif
```

:::

::: details **expserver/src/utils/xps_logger.c**

```c
#include "../xps.h"

void logger(xps_log_level_t level, const char *function_name, const char *format_string, ...) {
char *XPS_DEBUG = getenv("XPS_DEBUG");

if ((XPS_DEBUG == NULL || strcmp(XPS_DEBUG, "1") != 0) && level == LOG_DEBUG)
return;

const char *log_level_strings[] = {"ERROR", "INFO", "DEBUG", "WARNING", "HTTP"};
const char *log_level_colors[] = {RED_BG, BLUE_BG, MAGENTA_TEXT, YELLOW_BG, GREEN_BG};

va_list args;
va_start(args, format_string);

printf("%s" BOLD_START " %s " BOLD_END RESET_COLOR " " GREEN_TEXT "%s" RESET_COLOR " : ",
log_level_colors[level], log_level_strings[level], function_name);
vprintf(format_string, args);
printf("\n");

fflush(stdout);

va_end(args);
}
```
:::
## **Usage**
To log messages, you can use the `logger()` function provided by the module. Here's a basic example:
```c
void logger(xps_log_level_t level, const char *function_name, const char *format_string, ...);
```

- `level`: Log level of the message of type `xps_log_level_t`
- `function_name`: Name of the function logging the message
- `format_string`: Format string for the message
- Additional arguments can be passed similar to `printf` format

### **Environment Variable for Debugging**

Logging behavior can be controlled using the `XPS_DEBUG` environment variable. Log messages of level `DEBUG` will be printed on the terminal only when `XPS_DEBUG` is set to “1”.

```bash
export XPS_DEBUG=1
```

You can unset it using the command:

```bash
unset XPS_DEBUG
```

### Examples

Examples of logging errors of different levels:

```c
// ERROR
logger(LOG_ERROR, "xps_loop_create()", "epoll_create1() failed");

// INFO
logger(LOG_INFO, "listener_connection_handler()", "new connection");

// DEBUG
logger(LOG_DEBUG, "listener_connection_handler()", "make_socket_non_blocking() failed");

// WARNING
logger(LOG_WARNING, "sigint_handler()", "SIGINT received");

// HTTP
logger(LOG_HTTP, "xps_http()", "%s %s", http_req->method, http_req->path);
```
![logger.png](/assets/references/logger.png)
### Error levels
`xps_logger` provides predefined error levels with corresponding colors:
| Level | Color |
| ------- | ------------ |
| ERROR | RED_BG |
| INFO | BLUE_BG |
| DEBUG | MAGENTA_TEXT |
| WARNING | YELLOW_BG |
| HTTP | GREEN_BG |
## Extendibility
Users can extend `xps_logger` further by altering `xps_logger.h` and `xps_logger.c` to include additional logging error levels or alter existing ones.
Loading

0 comments on commit cc70104

Please sign in to comment.