9 changed files with 402 additions and 21 deletions
@ -0,0 +1,75 @@
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org> |
||||
* |
||||
* This file is part of ZToolkit |
||||
* |
||||
* ZToolkit is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* ZToolkit is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Affero Public License |
||||
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
/**
|
||||
* \file |
||||
* |
||||
* Logging utilities. |
||||
*/ |
||||
|
||||
#ifndef __Z_TOOLKIT_LOG_H__ |
||||
#define __Z_TOOLKIT_LOG_H__ |
||||
|
||||
typedef enum ZtkLogLevel |
||||
{ |
||||
ZTK_LOG_LEVEL_DEBUG, |
||||
ZTK_LOG_LEVEL_MESSAGE, |
||||
ZTK_LOG_LEVEL_WARNING, |
||||
ZTK_LOG_LEVEL_ERROR, |
||||
} ZtkLogLevel; |
||||
|
||||
static int ztk_log_enabled = 1; |
||||
|
||||
/**
|
||||
* Enables or disables logging. |
||||
*/ |
||||
static inline void |
||||
ztk_log_set_enabled ( |
||||
int enabled) |
||||
{ |
||||
ztk_log_enabled = enabled; |
||||
} |
||||
|
||||
/**
|
||||
* Logs a message. |
||||
* |
||||
* @param func Function name. |
||||
* @param level Log level. |
||||
* @param format The format of the message to log. |
||||
*/ |
||||
void |
||||
ztk_log ( |
||||
const char * func, |
||||
ZtkLogLevel level, |
||||
const char * format, |
||||
...); |
||||
|
||||
#define ztk_debug(msg) \ |
||||
ztk_log (__func__, ZTK_LOG_LEVEL_DEBUG, msg) |
||||
|
||||
#define ztk_message(msg) \ |
||||
ztk_log (__func__, ZTK_LOG_LEVEL_MESSAGE, msg) |
||||
|
||||
#define ztk_warning(msg) \ |
||||
ztk_log (__func__, ZTK_LOG_LEVEL_WARNING, msg) |
||||
|
||||
#define ztk_error(msg) \ |
||||
ztk_log (__func__, ZTK_LOG_LEVEL_ERROR, msg) |
||||
|
||||
#endif |
@ -0,0 +1,75 @@
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org> |
||||
* |
||||
* This file is part of ZToolkit |
||||
* |
||||
* ZToolkit is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* ZToolkit is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Affero Public License |
||||
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
/**
|
||||
* \file |
||||
* |
||||
* Widget for custom drawing. |
||||
*/ |
||||
|
||||
#ifndef __Z_TOOLKIT_ZTK_DRAWING_AREA_H__ |
||||
#define __Z_TOOLKIT_ZTK_DRAWING_AREA_H__ |
||||
|
||||
#include "ztoolkit/ztk_widget.h" |
||||
|
||||
/**
|
||||
* @addtogroup ztoolkit |
||||
* |
||||
* @{ |
||||
*/ |
||||
|
||||
/**
|
||||
* Drawing area widget. |
||||
* |
||||
* This is used for custom drawing. |
||||
*/ |
||||
typedef struct ZtkDrawingArea |
||||
{ |
||||
/** Base widget. */ |
||||
ZtkWidget base; |
||||
|
||||
} ZtkDrawingArea; |
||||
|
||||
/**
|
||||
* Creates a new ZtkDrawingArea. |
||||
* |
||||
* @param update_cb Optional callback to be called |
||||
* for updating the user data based on the current |
||||
* state of the widget. See ztk_knob.c for an |
||||
* example. |
||||
* @param draw_cb Draw callback. This will be called |
||||
* when the widget needs to redraw itself. |
||||
* @param dispose_cb Optional callback to be called |
||||
* when disposing the widget. This will be called |
||||
* right before freeing the widget. |
||||
* @param data User data. |
||||
*/ |
||||
ZtkDrawingArea * |
||||
ztk_drawing_area_new ( |
||||
PuglRect * rect, |
||||
ZtkWidgetCallback update_cb, |
||||
ZtkWidgetCallback draw_cb, |
||||
ZtkWidgetCallback dispose_cb, |
||||
void * data); |
||||
|
||||
/**
|
||||
* @} |
||||
*/ |
||||
|
||||
#endif |
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org> |
||||
* |
||||
* This file is part of ZToolkit |
||||
* |
||||
* ZToolkit is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* ZToolkit is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Affero Public License |
||||
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#include <stdarg.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <time.h> |
||||
#include <sys/time.h> |
||||
|
||||
#include <ztoolkit/log.h> |
||||
|
||||
/* ANSI color codes */ |
||||
#define COLOR_RED "\x1b[31m" |
||||
#define COLOR_GREEN "\x1b[32m" |
||||
#define COLOR_YELLOW "\x1b[33m" |
||||
#define COLOR_BLUE "\x1b[34m" |
||||
#define COLOR_MAGENTA "\x1b[35m" |
||||
#define COLOR_CYAN "\x1b[36m" |
||||
#define COLOR_RESET "\x1b[0m" |
||||
|
||||
/**
|
||||
* Fills in the timestamp as a string into \ref buf. |
||||
*/ |
||||
static void |
||||
get_timestamp ( |
||||
char * buf) |
||||
{ |
||||
struct timeval curTime; |
||||
gettimeofday(&curTime, NULL); |
||||
int milli = curTime.tv_usec / 1000; |
||||
|
||||
char buffer [80]; |
||||
strftime ( |
||||
buffer, 80, "%H:%M:%S", |
||||
localtime (&curTime.tv_sec)); |
||||
|
||||
sprintf(buf, "%s:%03d", buffer, milli); |
||||
} |
||||
|
||||
/**
|
||||
* Logs a message. |
||||
* |
||||
* @param func Function name. |
||||
* @param level Log level. |
||||
* @param format The format of the message to log. |
||||
*/ |
||||
void |
||||
ztk_log ( |
||||
const char * func, |
||||
ZtkLogLevel level, |
||||
const char * format, |
||||
...) |
||||
{ |
||||
va_list arg; |
||||
|
||||
char buf[6000] = "** "; |
||||
|
||||
switch (level) |
||||
{ |
||||
case ZTK_LOG_LEVEL_DEBUG: |
||||
strcat ( |
||||
buf, COLOR_CYAN "DEBUG: " COLOR_RESET); |
||||
break; |
||||
case ZTK_LOG_LEVEL_MESSAGE: |
||||
strcat ( |
||||
buf, |
||||
COLOR_GREEN "MESSAGE: " COLOR_RESET); |
||||
break; |
||||
case ZTK_LOG_LEVEL_WARNING: |
||||
strcat ( |
||||
buf, |
||||
COLOR_YELLOW "WARNING: " COLOR_RESET); |
||||
break; |
||||
case ZTK_LOG_LEVEL_ERROR: |
||||
strcat ( |
||||
buf, COLOR_RED "ERROR: " COLOR_RESET); |
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
|
||||
/* get current time as string*/ |
||||
char cur_time[200]; |
||||
get_timestamp (cur_time); |
||||
|
||||
strcat (buf, cur_time); |
||||
strcat (buf, ": "); |
||||
|
||||
/* format message */ |
||||
char log_msg[6000]; |
||||
va_start (arg, format); |
||||
vsprintf (log_msg, format, arg); |
||||
va_end (arg); |
||||
|
||||
strcat (buf, log_msg); |
||||
fprintf (stderr, "%s\n", buf); |
||||
} |
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org> |
||||
|
||||
* This file is part of ZToolkit |
||||
|
||||
* ZToolkit is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
|
||||
* ZToolkit is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
|
||||
* You should have received a copy of the GNU Affero General Public License |
||||
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#ifndef __Z_TOOLKIT_TESTS_HELPER_H__ |
||||
#define __Z_TOOLKIT_TESTS_HELPER_H__ |
||||
|
||||
#include <math.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
#include <ztoolkit/log.h> |
||||
|
||||
#define ztk_assert(x) \ |
||||
if (!(x)) \ |
||||
{ \ |
||||
ztk_error ("Assertion failed: %s", #x); \ |
||||
exit (1); \ |
||||
} |
||||
|
||||
int |
||||
str_endswith ( |
||||
const char *s, const char *t) |
||||
{ |
||||
size_t ls = strlen(s); // find length of s
|
||||
size_t lt = strlen(t); // find length of t
|
||||
if (ls >= lt) // check if t can fit in s
|
||||
{ |
||||
// point s to where t should start and compare the strings from there
|
||||
return (0 == memcmp(t, s + (ls - lt), lt)); |
||||
} |
||||
return 0; // t was longer than s
|
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org> |
||||
* |
||||
* This file is part of ZToolkit |
||||
* |
||||
* ZToolkit is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* ZToolkit is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Affero General Public License |
||||
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#include "helper.h" |
||||
|
||||
int main ( |
||||
int argc, const char* argv[]) |
||||
{ |
||||
ztk_debug ("debug test"); |
||||
ztk_message ("message test"); |
||||
ztk_warning ("warning test"); |
||||
ztk_error ("error test"); |
||||
|
||||
return 0; |
||||
} |
Loading…
Reference in new issue