Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 1 | /* |
| 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 Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <stdarg.h> |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 13 | #include <stdint.h> |
David Sodman | e46ea8a | 2015-03-13 15:47:12 -0700 | [diff] [blame] | 14 | #include <stdbool.h> |
Chris Morin | 255d9f0 | 2019-02-04 23:29:27 -0800 | [diff] [blame] | 15 | #include <syslog.h> |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 16 | #include <time.h> |
Chris Morin | 255d9f0 | 2019-02-04 23:29:27 -0800 | [diff] [blame] | 17 | #include <unistd.h> |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 18 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 19 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 20 | #define MIN(A, B) ((A) < (B) ? (A) : (B)) |
| 21 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 22 | #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A))) |
| 23 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 24 | #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 Gupta | 755f3ca | 2019-05-09 18:23:24 -0700 | [diff] [blame] | 29 | static inline int64_t get_monotonic_time_ms() { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 30 | 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 Boichat | ec62b15 | 2021-03-22 15:24:48 +0800 | [diff] [blame] | 35 | #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 Morin | 255d9f0 | 2019-02-04 23:29:27 -0800 | [diff] [blame] | 50 | |
Dominik Behr | 09f2b72 | 2016-10-05 12:02:04 -0700 | [diff] [blame] | 51 | void daemonize(bool wait_child); |
| 52 | void daemon_exit_code(char code); |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 53 | void parse_location(char* loc_str, int* x, int* y); |
| 54 | void parse_filespec(char* filespec, char* filename, |
| 55 | int32_t* offset_x, int32_t* offset_y, uint32_t* duration, |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 56 | uint32_t default_duration, int32_t default_x, int32_t default_y); |
| 57 | void parse_image_option(char* optionstr, char** name, char** val); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 58 | |
Dominik Behr | 2b9f123 | 2016-08-02 01:11:12 -0700 | [diff] [blame] | 59 | /* make sure stdio file descriptors are somewhat sane */ |
| 60 | void fix_stdio(void); |
Dominik Behr | da6df41 | 2016-08-02 12:56:42 -0700 | [diff] [blame] | 61 | bool write_string_to_file(const char *path, const char *s); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 62 | |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 63 | #endif |