Skip to content

Commit

Permalink
chore: Include log.c library in codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
pkwagner committed Mar 28, 2024
1 parent 2ae490d commit cebcb90
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 26 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ FROM debian:bullseye-slim
COPY . /yasdi2mqtt

RUN apt-get update && apt-get install -y git gcc make cmake openssl libssl-dev libcjson1 libcjson-dev libpaho-mqtt1.3 libpaho-mqtt-dev \
&& git clone --depth=1 https://github.com/rxi/log.c.git logc \
&& gcc -shared -fPIC -DLOG_USE_COLOR -o /usr/local/lib/liblog_c.so logc/src/log.c && cp logc/src/*.h /usr/local/include \
&& git clone --depth=1 https://github.com/pkwagner/yasdi.git yasdi \
&& mkdir yasdi/projects/generic-cmake/build-gcc && cd yasdi/projects/generic-cmake/build-gcc \
&& cmake -D YASDI_DEBUG_OUTPUT=0 .. && make && make install \
&& cd ../../../.. \
&& cd yasdi2mqtt && make YASDI_PATH=../yasdi && make YASDI_PATH=../yasdi install && cd .. \
&& rm -rf logc yasdi \
&& rm -rf yasdi \
&& apt-get purge -y --auto-remove git gcc make cmake libssl-dev libcjson-dev libpaho-mqtt-dev && rm -rf /var/lib/apt/lists/* \
&& mkdir /etc/yasdi2mqtt

Expand Down
8 changes: 2 additions & 6 deletions Dockerfile.alpine
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ COPY . /yasdi2mqtt
RUN apk add --no-cache git gcc musl-dev make cmake openssl-dev cjson-dev
RUN ln -s termios.h /usr/include/termio.h

# Prepare DESTDIR for log.c and yasdi2mqtt
RUN mkdir -p /target/usr/local/lib /target/usr/local/bin
# Prepare DESTDIR for yasdi2mqtt
RUN mkdir -p /target/usr/local/bin

# Install Paho
RUN git clone --depth=1 https://github.com/eclipse/paho.mqtt.c.git paho
RUN mkdir paho/build && cd paho/build && cmake -DPAHO_WITH_SSL=TRUE .. && make && make DESTDIR=/target install

# Install log.c
RUN git clone --depth=1 https://github.com/rxi/log.c.git logc
RUN cd logc && gcc -shared -fPIC -DLOG_USE_COLOR -o /target/usr/local/lib/liblog_c.so src/log.c && cp src/*.h /target/usr/local/include

# Install YASDI
RUN git clone --depth=1 https://github.com/pkwagner/yasdi.git yasdi
RUN mkdir yasdi/projects/generic-cmake/build
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ YASDI_PATH=yasdi
DESTDIR=/usr/local/bin

IDIRS=-I$(YASDI_PATH)/libs -I$(YASDI_PATH)/include -I$(YASDI_PATH)/core -I$(YASDI_PATH)/driver -I$(YASDI_PATH)/smalib -I$(YASDI_PATH)/master -I$(YASDI_PATH)/os
SRC=src/main.c src/yasdi_handler.c src/mqtt_client.c
SRC=src/main.c src/yasdi_handler.c src/mqtt_client.c src/log.c

CC=gcc
CFLAGS=-std=c11 $(IDIRS)
DEPS=-lyasdimaster -llog_c -lcjson -lpaho-mqtt3cs
CFLAGS=-std=c11 -DLOG_USE_COLOR $(IDIRS)
DEPS=-lyasdimaster -lcjson -lpaho-mqtt3cs

ifeq ($(DEBUG), 1)
CFLAGS+= -g
Expand All @@ -16,4 +16,4 @@ yasdi2mqtt: $(SRC)
$(CC) -o yasdi2mqtt $(SRC) $(CFLAGS) $(DEPS)

install: yasdi2mqtt
cp yasdi2mqtt $(DESTDIR)
cp yasdi2mqtt $(DESTDIR)
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,22 @@ docker run \

1. Install [cJSON](https://github.com/DaveGamble/cJSON), [Paho](https://github.com/eclipse/paho.mqtt.c.git), and other project dependencies
1. `sudo apt install git gcc make cmake openssl libssl-dev libcjson1 libcjson-dev libpaho-mqtt1.3 libpaho-mqtt-dev` (assuming an Ubuntu-based OS)
2. Clone and install [log.c](https://github.com/rxi/log.c.git)
1. `gcc -shared -fPIC -DLOG_USE_COLOR -o liblog_c.so src/log.c`
2. `sudo cp liblog_c.so /usr/local/lib` for system-wide installation
3. `sudo cp src/*.h /usr/local/include` to make the headers available system-wide
3. Clone and install [YASDI](https://github.com/konstantinblaesi/yasdi.git)
2. Clone and install [YASDI](https://github.com/konstantinblaesi/yasdi.git)
1. `mkdir projects/generic-cmake/build-gcc`
2. `cd projects/generic-cmake/build-gcc`
3. `cmake -D YASDI_DEBUG_OUTPUT=0 ..`
4. `make`
5. `sudo make install`
4. Clone and install this repository
3. Clone and install this repository
1. `make YASDI_PATH=<yasdi_dir>`
2. `sudo make YASDI_PATH=<yasdi_dir> install`
5. Check `yasdi.ini` configuration
4. Check `yasdi.ini` configuration
* The included blueprint is pre-configured for directly attached RS485 adapters, but you may want to adjust the serial adapter path.
* If you want to use IP-based communication instead, you should have a look on the `yasdi` manual or check out [this discussion](https://github.com/pkwagner/yasdi2mqtt/issues/1) (German).
6. Create empty `devices` directory
5. Create empty `devices` directory
* Not necessary, but `yasdi` will use this folder as device data cache, so you'll save 1-2 minutes for device data download after the first startup.
7. Set environment variables according to the table below
8. `./yasdi2mqtt`
6. Set environment variables according to the table below
7. `./yasdi2mqtt`
* After starting up, `yasdi2mqtt` should immediately connect to your MQTT broker.
* Detected devices should be printed on `stdout` quite quickly, but the the initial device data download may delay the first data for 1-2 minutes.
</details>
Expand Down Expand Up @@ -146,3 +142,6 @@ There are two options to make `yasdi2mqtt` more verbose:
* Replace `YASDI_DEBUG_OUTPUT=0` by `YASDI_DEBUG_OUTPUT=1` in the `Dockerfile` to activate the debug output of the underlying `yasdi` library.
* You'll need to re-compile your own container using `docker build -t yasdi2mqtt .` and change the `docker-compose.yml` file to use your own container.
* When doing a manual setup, replace the parameter directly when installing `yasdi`.

## License
Unless otherwise stated in the file header, all contributions to this project are licensed under the MIT license. In particular, `src/log.c` and `src/log.h` are NOT part of this project.
168 changes: 168 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2020 rxi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "log.h"

#define MAX_CALLBACKS 32

typedef struct {
log_LogFn fn;
void *udata;
int level;
} Callback;

static struct {
void *udata;
log_LockFn lock;
int level;
bool quiet;
Callback callbacks[MAX_CALLBACKS];
} L;


static const char *level_strings[] = {
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
};

#ifdef LOG_USE_COLOR
static const char *level_colors[] = {
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
};
#endif


static void stdout_callback(log_Event *ev) {
char buf[16];
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
#ifdef LOG_USE_COLOR
fprintf(
ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
buf, level_colors[ev->level], level_strings[ev->level],
ev->file, ev->line);
#else
fprintf(
ev->udata, "%s %-5s %s:%d: ",
buf, level_strings[ev->level], ev->file, ev->line);
#endif
vfprintf(ev->udata, ev->fmt, ev->ap);
fprintf(ev->udata, "\n");
fflush(ev->udata);
}


static void file_callback(log_Event *ev) {
char buf[64];
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
fprintf(
ev->udata, "%s %-5s %s:%d: ",
buf, level_strings[ev->level], ev->file, ev->line);
vfprintf(ev->udata, ev->fmt, ev->ap);
fprintf(ev->udata, "\n");
fflush(ev->udata);
}


static void lock(void) {
if (L.lock) { L.lock(true, L.udata); }
}


static void unlock(void) {
if (L.lock) { L.lock(false, L.udata); }
}


const char* log_level_string(int level) {
return level_strings[level];
}


void log_set_lock(log_LockFn fn, void *udata) {
L.lock = fn;
L.udata = udata;
}


void log_set_level(int level) {
L.level = level;
}


void log_set_quiet(bool enable) {
L.quiet = enable;
}


int log_add_callback(log_LogFn fn, void *udata, int level) {
for (int i = 0; i < MAX_CALLBACKS; i++) {
if (!L.callbacks[i].fn) {
L.callbacks[i] = (Callback) { fn, udata, level };
return 0;
}
}
return -1;
}


int log_add_fp(FILE *fp, int level) {
return log_add_callback(file_callback, fp, level);
}


static void init_event(log_Event *ev, void *udata) {
if (!ev->time) {
time_t t = time(NULL);
ev->time = localtime(&t);
}
ev->udata = udata;
}


void log_log(int level, const char *file, int line, const char *fmt, ...) {
log_Event ev = {
.fmt = fmt,
.file = file,
.line = line,
.level = level,
};

lock();

if (!L.quiet && level >= L.level) {
init_event(&ev, stderr);
va_start(ev.ap, fmt);
stdout_callback(&ev);
va_end(ev.ap);
}

for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) {
Callback *cb = &L.callbacks[i];
if (level >= cb->level) {
init_event(&ev, cb->udata);
va_start(ev.ap, fmt);
cb->fn(&ev);
va_end(ev.ap);
}
}

unlock();
}
49 changes: 49 additions & 0 deletions src/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2020 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See `log.c` for details.
*/

#ifndef LOG_H
#define LOG_H

#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <time.h>

#define LOG_VERSION "0.1.0"

typedef struct {
va_list ap;
const char *fmt;
const char *file;
struct tm *time;
void *udata;
int line;
int level;
} log_Event;

typedef void (*log_LogFn)(log_Event *ev);
typedef void (*log_LockFn)(bool lock, void *udata);

enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };

#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)

const char* log_level_string(int level);
void log_set_lock(log_LockFn fn, void *udata);
void log_set_level(int level);
void log_set_quiet(bool enable);
int log_add_callback(log_LogFn fn, void *udata, int level);
int log_add_fp(FILE *fp, int level);

void log_log(int level, const char *file, int line, const char *fmt, ...);

#endif
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <log.h>
#include <cjson/cJSON.h>
#include "yasdi_handler.h"
#include "mqtt_client.h"
#include "log.h"

char *get_required_env(const char *name);
void new_values_cb(struct device_value_t *values);
Expand Down
2 changes: 1 addition & 1 deletion src/mqtt_client.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "mqtt_client.h"
#include "log.h"

#include <unistd.h>
#include <string.h>
#include <log.h>
#include <MQTTClient.h>

#define MQTT_KEEP_ALIVE_INTERVAL 20
Expand Down
2 changes: 1 addition & 1 deletion src/yasdi_handler.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "yasdi_handler.h"
#include "log.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <log.h>
#include <libyasdimaster.h>

#define MAX_DRIVER_COUNT 128
Expand Down

0 comments on commit cebcb90

Please sign in to comment.