blob: 3d4338573b9b5c191113f25f7596ec0f4f63e537 [file] [log] [blame]
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -07001/*
2 * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#ifndef UTIL_H
8#define UTIL_H
9
David Sodmanbbcb0522014-09-19 10:34:07 -070010#include <stdio.h>
11#include <stdlib.h>
12#include <stdarg.h>
David Sodman8ef20062015-01-06 09:23:40 -080013#include <stdint.h>
David Sodmane46ea8a2015-03-13 15:47:12 -070014#include <stdbool.h>
Chris Morin255d9f02019-02-04 23:29:27 -080015#include <syslog.h>
David Sodmanbbcb0522014-09-19 10:34:07 -070016#include <time.h>
Chris Morin255d9f02019-02-04 23:29:27 -080017#include <unistd.h>
David Sodmanbbcb0522014-09-19 10:34:07 -070018
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070019#define MAX(A, B) ((A) > (B) ? (A) : (B))
David Sodman8ef20062015-01-06 09:23:40 -080020#define MIN(A, B) ((A) < (B) ? (A) : (B))
21
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070022#define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A)))
23
David Sodmanbbcb0522014-09-19 10:34:07 -070024#define MS_PER_SEC (1000LL)
25#define NS_PER_SEC (1000LL * 1000LL * 1000LL)
26#define NS_PER_MS (NS_PER_SEC / MS_PER_SEC);
27
28/* Returns the current CLOCK_MONOTONIC time in milliseconds. */
Manoj Gupta755f3ca2019-05-09 18:23:24 -070029static inline int64_t get_monotonic_time_ms() {
David Sodmanbbcb0522014-09-19 10:34:07 -070030 struct timespec spec;
31 clock_gettime(CLOCK_MONOTONIC, &spec);
32 return MS_PER_SEC * spec.tv_sec + spec.tv_nsec / NS_PER_MS;
33}
34
Nicolas Boichatec62b152021-03-22 15:24:48 +080035#define ERROR (LOG_ERR)
36#define WARNING (LOG_WARNING)
37#define INFO (LOG_INFO)
38#define DEBUG (LOG_DEBUG)
39
40/* Don't print debug messages by default. */
41#ifndef LOG_LEVEL
42#define LOG_LEVEL (LOG_DEBUG-1)
43#endif
44
45#define LOG(severity, fmt, ...) do { \
46 if (severity <= LOG_LEVEL) \
47 fprintf(stderr, "<%i>frecon[%d]: " fmt "\n", \
48 severity, getpid(), ##__VA_ARGS__); \
49 } while (0)
Chris Morin255d9f02019-02-04 23:29:27 -080050
Dominik Behr09f2b722016-10-05 12:02:04 -070051void daemonize(bool wait_child);
52void daemon_exit_code(char code);
Stéphane Marchesin00ff1872015-12-14 13:40:09 -080053void parse_location(char* loc_str, int* x, int* y);
54void parse_filespec(char* filespec, char* filename,
55 int32_t* offset_x, int32_t* offset_y, uint32_t* duration,
David Sodman8ef20062015-01-06 09:23:40 -080056 uint32_t default_duration, int32_t default_x, int32_t default_y);
57void parse_image_option(char* optionstr, char** name, char** val);
David Sodmanbbcb0522014-09-19 10:34:07 -070058
Dominik Behr2b9f1232016-08-02 01:11:12 -070059/* make sure stdio file descriptors are somewhat sane */
60void fix_stdio(void);
Dominik Behrda6df412016-08-02 12:56:42 -070061bool write_string_to_file(const char *path, const char *s);
David Sodmanbbcb0522014-09-19 10:34:07 -070062
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070063#endif