Skip to content

Commit

Permalink
logger file descriptor is now duped
Browse files Browse the repository at this point in the history
nickeldan committed May 25, 2021
1 parent dfdd9b2 commit 277ad22
Showing 4 changed files with 62 additions and 16 deletions.
29 changes: 16 additions & 13 deletions README.rst
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ Vanilla Squad

:Author: Daniel Walker

Version 4.1.0 was released on May 16, 2021.
Version 4.2.0 was released on May 24, 2021.

Overview
========
@@ -20,9 +20,9 @@ size of the destination is zero, they return -1. Otherwise, they return the num
written to the destination (NOT counting the terminator). If a -1 is not returned, then a terminator is
guaranteed to be written to the destination.

**vasqIncSnprintf** and **vasqIncVsnprintf** function similarly except that they take pointers to the destination as
well as the size of the destination. Upon success, they adjust the destination and size so that subsequent
calls to these functions will pick up where the previous call left off.
**vasqIncSnprintf** and **vasqIncVsnprintf** function similarly except that they take pointers to the
destination as well as the size of the destination. Upon success, they adjust the destination and size so
that subsequent calls to these functions will pick up where the previous call left off.

The % tokens recognized by these functions are

@@ -53,13 +53,13 @@ Logging

The provided logging levels (which are of type **vasqLogLevel_t**) are

* VASQ_LL_NONE
* VASQ_LL_ALWAYS
* VASQ_LL_CRITICAL
* VASQ_LL_ERROR
* VASQ_LL_WARNING
* VASQ_LL_INFO
* VASQ_LL_DEBUG
* **VASQ_LL_NONE**
* **VASQ_LL_ALWAYS**
* **VASQ_LL_CRITICAL**
* **VASQ_LL_ERROR**
* **VASQ_LL_WARNING**
* **VASQ_LL_INFO**
* **VASQ_LL_DEBUG**

A logger handle is created by the **vasqLoggerCreate** function. Its signature is

@@ -75,8 +75,11 @@ A logger handle is created by the **vasqLoggerCreate** function. Its signature
vasqLogger **logger, // A pointer to the logger handle to be populated.
);
This function returns **VASQ_RET_OK** when successful and an error code otherwise (see include/vasq/definitions.h
for the values).
This function returns **VASQ_RET_OK** when successful and an error code otherwise (see
include/vasq/definitions.h for the values).

**vasqLoggerCreate** calls **dup** on the provided file descriptor so that you may call **close** while still
maintaining logging. The new descriptor has **FD_CLOEXEC** set on it.

The format string looks like a **printf** string and accepts the following % tokens:

3 changes: 3 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
4.2.0:
- vasqLoggerCreate now dups the file descriptor and sets FD_CLOEXEC on it.

4.1.0:
- Various logging limits are now configurable via config.h.
- Added vasqExit.
5 changes: 4 additions & 1 deletion include/vasq/definitions.h
Original file line number Diff line number Diff line change
@@ -16,11 +16,14 @@
enum vasqRetValue {
VASQ_RET_OK = 0,
VASQ_RET_USAGE,
VASQ_RET_BAD_FORMAT,
VASQ_RET_OUT_OF_MEMORY,
VASQ_RET_DUP_FAIL,
VASQ_RET_FCNTL_FAIL,

VASQ_RET_UNUSED,
};

#define VASQ_VERSION "4.1.0"
#define VASQ_VERSION "4.2.0"

#endif // VANILLA_SQUAD_DEFINITIONS_H
41 changes: 39 additions & 2 deletions source/logger.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -93,28 +94,64 @@ int
vasqLoggerCreate(int fd, vasqLogLevel_t level, const char *format, vasqLoggerDataProcessor processor,
void *user_data, vasqLogger **logger)
{
if (fd < 0 || !logger || !format || !validLogFormat(format)) {
int new_fd, flags;

if (fd < 0 || !logger || !format) {
return VASQ_RET_USAGE;
}

if (!validLogFormat(format)) {
return VASQ_RET_BAD_FORMAT;
}

*logger = malloc(sizeof(**logger));
if (!*logger) {
return VASQ_RET_OUT_OF_MEMORY;
}

(*logger)->fd = fd;
while (true) {
new_fd = dup(fd);
if (new_fd == -1) {
int local_errno = errno;

switch (local_errno) {
#ifdef EBUSY
case EBUSY:
#endif
case EINTR: continue;

default:
fprintf(stderr, "dup: %s", strerror(local_errno));
free(*logger);
return VASQ_RET_DUP_FAIL;
}
}
else {
break;
}
}

(*logger)->fd = new_fd;
(*logger)->level = level;
(*logger)->format = format;
(*logger)->processor = processor;
(*logger)->user_data = user_data;

flags = fcntl(new_fd, F_GETFD);
if (flags == -1 || fcntl(new_fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
perror("fcntl");
vasqLoggerFree(*logger);
return VASQ_RET_FCNTL_FAIL;
}

return VASQ_RET_OK;
}

void
vasqLoggerFree(vasqLogger *logger)
{
if (logger) {
close(logger->fd);
free(logger);
}
}

0 comments on commit 277ad22

Please sign in to comment.