Commit 07d01c79 authored by xpetrak2's avatar xpetrak2
Browse files

Simple logger and configuration of files and dirs

parent 1f9e5d67
......@@ -4,7 +4,10 @@ bin_PROGRAMS = src/coincerd src/coincer
src_coincerd_SOURCES = \
src/coincerd.c \
src/configuration.c src/configuration.h \
src/filing.c src/filing.h \
src/linkedlist.c src/linkedlist.h \
src/log.c src/log.h \
src/neighbours.c src/neighbours.h \
src/p2p.c src/p2p.h
......
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "autoconfig.h"
#include "configuration.h"
#include "log.h"
/**
* Set home directory.
*
* @param homedir Path to homedir.
*
* @return 0 Path successfully set.
* @return 1 Home directory retrieval failure.
*/
static int set_homedir(char **homedir)
{
*homedir = getenv("HOME");
if (*homedir == NULL || *homedir[0] == '\0') {
log_error("set_homedir - cannot find home directory");
return 1;
}
return 0;
}
/**
* Set paths to config and data directories.
*
* @param config_dir Config directory.
* @param data_dir Data directory.
*
* @return 0 Paths successfully set.
* @return 1 Allocation failure.
*/
static int set_directories(char **config_dir, char **data_dir)
{
char *homedir = NULL,
*tmpchar = NULL;
size_t tmpsize;
/* configuration directory */
tmpchar = getenv("XDG_CONFIG_HOME");
if (tmpchar == NULL || tmpchar[0] == '\0') {
if (set_homedir(&homedir)) {
return 1;
}
tmpsize = strlen(homedir);
*config_dir = (char *) malloc(tmpsize +
sizeof("/.config/" PACKAGE_NAME
"/"));
if (*config_dir == NULL) {
log_error("set_directories - setting config_dir");
return 1;
}
strcpy(*config_dir, homedir);
strcpy(*config_dir + tmpsize, "/.config/" PACKAGE_NAME "/");
} else {
tmpsize = strlen(tmpchar);
*config_dir = (char *) malloc(tmpsize +
sizeof("/" PACKAGE_NAME "/"));
if (*config_dir == NULL) {
log_error("set_directories - setting config_dir");
return 1;
}
strcpy(*config_dir, tmpchar);
strcpy(*config_dir + tmpsize, "/" PACKAGE_NAME "/");
}
/* data directory */
tmpchar = getenv("XDG_DATA_HOME");
if (tmpchar == NULL || tmpchar[0] == '\0') {
if (homedir == NULL && set_homedir(&homedir)) {
free(config_dir);
return 1;
}
tmpsize = strlen(homedir);
*data_dir = (char *) malloc(tmpsize +
sizeof("/.local/share/" PACKAGE_NAME
"/"));
if (*data_dir == NULL) {
log_error("set_directories - setting data_dir");
free(config_dir);
return 1;
}
strcpy(*data_dir, homedir);
strcpy(*data_dir + tmpsize, "/.local/share/" PACKAGE_NAME "/");
} else {
tmpsize = strlen(tmpchar);
*data_dir = (char *) malloc(tmpsize +
sizeof("/" PACKAGE_NAME "/"));
if (*data_dir == NULL) {
log_error("set_directories - setting data_dir");
free(config_dir);
return 1;
}
strcpy(*data_dir, tmpchar);
strcpy(*data_dir + tmpsize, "/" PACKAGE_NAME "/");
}
return 0;
}
/**
* Creates directories if they don't exist yet.
*
* @param config_dir Config directory.
* @param data_dir Data directory.
*
* @return 0 Directories created.
* @return 1 Creating directories failed.
*/
static int create_dirs(const char *config_dir, const char *data_dir)
{
struct stat buffer;
/* configuration directory */
if (stat(config_dir, &buffer)) {
if (errno == ENOENT) {
/* create */
if (mkdir(config_dir, S_IRWXU)) {
log_error("create_dirs - could not create "
"configuration directory %s",
config_dir);
return 1;
} else {
log_debug("create_dirs - created configuration "
"directory %s", config_dir);
}
} else {
log_error("create_dirs - could not open configuration "
"directory %s", config_dir);
return 1;
}
}
/* data directory */
if (stat(data_dir, &buffer)) {
if (errno == ENOENT) {
/* create */
if (mkdir(data_dir, S_IRWXU)) {
log_error("create_dirs - could not create "
"data directory %s",
data_dir);
return 1;
} else {
log_debug("create_dirs - created data "
"directory %s", data_dir);
}
} else {
log_error("create_dirs - could not open data "
"directory %s", data_dir);
return 1;
}
}
return 0;
}
/**
* Create needed directories and fetch their paths into params.
*
* @param config_dir Config directory.
* @param data_dir Data directory.
*
* @return 0 Directories created.
* @return 1 Directories setup failure.
*/
int setup_directories(char **config_dir, char **data_dir)
{
if (set_directories(config_dir, data_dir) ||
create_dirs(*config_dir, *data_dir)) {
return 1;
}
return 0;
}
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
int setup_directories(char **config_dir, char **data_dir);
#endif /* CONFIGURATION_H */
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h> /* chmod */
#include "configuration.h"
#include "filing.h"
#include "log.h"
static const char *PEERS_FILE_NAME = "peers";
/**
* Sets path to peers file.
*
* @param data_dir Path to data dir.
* @param peers The file path of peers.
*
* @return 0 Path successfully set.
* @return 1 Allocation failure.
*/
static int set_peers_path(char *data_dir, char **peers)
{
FILE *peers_file;
/* size of data_dir + PEERS_FILE_NAME + '\0' */
*peers = (char *) malloc(strlen(data_dir) +
strlen(PEERS_FILE_NAME) + 1);
if (*peers == NULL) {
log_error("set_peers_path - peers file malloc");
return 1;
}
strcpy(*peers, data_dir);
strcat(*peers, PEERS_FILE_NAME);
/* create file if doesn't exist */
peers_file = fopen(*peers, "a");
fclose(peers_file);
/* change permissions */
chmod(*peers, strtol("0600", 0, 8));
return 0;
}
/**
* Initializes a filepaths_t instance.
*
* @param filepaths filepaths_t to be initialized.
*
* @return 0 Successfully initialized.
* @return 1 Setup failure.
*/
int setup_paths(filepaths_t *filepaths)
{
if (setup_directories(&filepaths->config_dir,
&filepaths->data_dir)) {
return 1;
}
set_peers_path(filepaths->data_dir, &filepaths->peers);
return 0;
}
/**
* Clear allocated path strings.
*
* @param filepaths filepaths_t to be cleared.
*/
void clear_paths(filepaths_t *filepaths)
{
free(filepaths->config_dir);
free(filepaths->data_dir);
free(filepaths->peers);
}
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILING_H
#define FILING_H
#include <arpa/inet.h>
#include <stdint.h>
/** Paths to needed files and directories. */
typedef struct s_filepaths {
/**< Path to config directory. */
char *config_dir;
/**< Path to data directory. */
char *data_dir;
/**< Path to file with addresses of peers. */
char *peers;
} filepaths_t;
void clear_paths(filepaths_t *filepaths);
int setup_paths(filepaths_t *filepaths);
#endif /* FILING_H */
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdio.h>
#include "autoconfig.h"
#include "log.h"
/**
* Logs debugging stuff to stdout, but only when compiled with debug enabled.
*
* @param msg Formatted message.
* @param ... Parameters to be included in the message.
*/
void log_debug(const char *msg, ...)
{
#ifdef DEBUG
va_list args;
fprintf(stdout, "[Debug] ");
va_start(args, msg);
vfprintf(stdout, msg, args);
va_end(args);
fprintf(stdout, "\n");
#endif /* DEBUG */
}
/**
* Logs information to stdout.
*
* @param msg Formatted message.
* @param ... Parameters to be included in the message.
*/
void log_info(const char *msg, ...)
{
va_list args;
fprintf(stdout, "[Info] ");
va_start(args, msg);
vfprintf(stdout, msg, args);
va_end(args);
fprintf(stdout, "\n");
}
/**
* Logs warnings to stderr.
*
* @param msg Formatted message.
* @param ... Parameters to be included in the message.
*/
void log_warn(const char *msg, ...)
{
va_list args;
fprintf(stderr, "[Warning] ");
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, "\n");
}
/**
* Logs errors to stderr.
*
* @param msg Formatted message.
* @param ... Parameters to be included in the message.
*/
void log_error(const char *msg, ...)
{
va_list args;
fprintf(stderr, "[Error] ");
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, "\n");
}
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LOG_H
#define LOG_H
void log_debug(const char *msg, ...);
void log_info(const char *msg, ...);
void log_warn(const char *msg, ...);
void log_error(const char *msg, ...);
#endif /* LOG_H */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment