blob: e1ba8ebfc66444e0bff9517b9597839d97e076d4 [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
Chris Morin255d9f02019-02-04 23:29:27 -080035#define LOG(severity, fmt, ...) (fprintf(stderr, "<%i>frecon[%d]: " fmt "\n", \
36 severity, getpid(), ##__VA_ARGS__))
37
Dominik Behr09f2b722016-10-05 12:02:04 -070038void daemonize(bool wait_child);
39void daemon_exit_code(char code);
Stéphane Marchesin00ff1872015-12-14 13:40:09 -080040void parse_location(char* loc_str, int* x, int* y);
41void parse_filespec(char* filespec, char* filename,
42 int32_t* offset_x, int32_t* offset_y, uint32_t* duration,
David Sodman8ef20062015-01-06 09:23:40 -080043 uint32_t default_duration, int32_t default_x, int32_t default_y);
44void parse_image_option(char* optionstr, char** name, char** val);
David Sodmanbbcb0522014-09-19 10:34:07 -070045
Dominik Behr2b9f1232016-08-02 01:11:12 -070046/* make sure stdio file descriptors are somewhat sane */
47void fix_stdio(void);
Dominik Behrda6df412016-08-02 12:56:42 -070048bool write_string_to_file(const char *path, const char *s);
David Sodmanbbcb0522014-09-19 10:34:07 -070049
Chris Morin255d9f02019-02-04 23:29:27 -080050#define ERROR (LOG_ERR)
51#define WARNING (LOG_WARNING)
52#define INFO (LOG_INFO)
David Sodmanbbcb0522014-09-19 10:34:07 -070053
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070054#endif