summaryrefslogtreecommitdiffstats
path: root/src/libbbsutil/log.c
blob: 9fe3d5141424b22e375cab756a8cfa5b45666747 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdarg.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include "libbbsutil.h"

int
log_filef(const char *fn, int log_flag, const char *fmt,...)
{
    char       msg[256];

    va_list ap;
    va_start(ap, fmt);
    vsnprintf(msg, sizeof(msg), fmt, ap);
    va_end(ap);

    return log_file(fn, log_flag, msg);
}

int
log_file(const char *fn, int log_flag, const char *msg)
{
    int        fd;
    int flag = O_APPEND | O_WRONLY;
    int mode = 0664;

    if (log_flag & LOG_CREAT)
    flag |= O_CREAT;

    fd = open(fn, flag, mode);
    if( fd < 0 )
    return -1;

    if( write(fd, msg, strlen(msg)) < 0 ){
    close(fd);
    return -1;
    }
    close(fd);
    return 0;
}