vdr-plugin-softhdcuvid/misc.h

174 lines
4.6 KiB
C
Raw Permalink Normal View History

2018-08-19 11:45:46 +02:00
///
/// @file misc.h @brief Misc function header file
2018-08-19 11:45:46 +02:00
///
/// Copyright (c) 2009 - 2012 by Lutz Sammer. All Rights Reserved.
2018-08-19 11:45:46 +02:00
///
/// Contributor(s):
/// Copied from uwm.
2018-08-19 11:45:46 +02:00
///
/// License: AGPLv3
2018-08-19 11:45:46 +02:00
///
/// This program 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.
2018-08-19 11:45:46 +02:00
///
/// 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 Affero General Public License for more details.
2018-08-19 11:45:46 +02:00
///
/// $Id: f5ff4b300aa33eb721d658c0c9374c8499b67318 $
2018-08-19 11:45:46 +02:00
//////////////////////////////////////////////////////////////////////////////
/// @addtogroup misc
/// @{
#include <stdarg.h>
#include <syslog.h>
#include <time.h> // clock_gettime
2018-08-19 11:45:46 +02:00
//////////////////////////////////////////////////////////////////////////////
// Defines
2018-08-19 11:45:46 +02:00
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Declares
2018-08-19 11:45:46 +02:00
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Variables
2018-08-19 11:45:46 +02:00
//////////////////////////////////////////////////////////////////////////////
extern int SysLogLevel; ///< how much information wanted
2018-08-19 11:45:46 +02:00
//////////////////////////////////////////////////////////////////////////////
// Prototypes
2018-08-19 11:45:46 +02:00
//////////////////////////////////////////////////////////////////////////////
static inline void Syslog(const int, const char *format, ...) __attribute__((format(printf, 2, 3)));
2018-08-19 11:45:46 +02:00
//////////////////////////////////////////////////////////////////////////////
// Inlines
2018-08-19 11:45:46 +02:00
//////////////////////////////////////////////////////////////////////////////
#ifdef DEBUG
#define DebugLevel 4 /// private debug level
2018-08-19 11:45:46 +02:00
#else
#define DebugLevel 0 /// private debug level
2018-08-19 11:45:46 +02:00
#endif
/**
** Syslog output function.
**
** - 0 fatal errors and errors
** - 1 warnings
** - 2 info
** - 3 important debug and fixme's
*/
static inline void Syslog(const int level, const char *format, ...) {
2018-08-19 11:45:46 +02:00
if (SysLogLevel > level || DebugLevel > level) {
va_list ap;
2018-08-19 11:45:46 +02:00
va_start(ap, format);
vsyslog(LOG_ERR, format, ap);
va_end(ap);
2018-08-19 11:45:46 +02:00
}
}
/**
** Show error.
*/
#define Error(fmt...) Syslog(LOG_ERR, fmt)
2018-08-19 11:45:46 +02:00
/**
** Show fatal error.
*/
#define Fatal(fmt...) \
do { \
Error(fmt); \
abort(); \
} while (0)
2018-08-19 11:45:46 +02:00
/**
** Show warning.
*/
#define Warning(fmt...) Syslog(LOG_WARNING, fmt)
2018-08-19 11:45:46 +02:00
/**
** Show info.
*/
#define Info(fmt...) Syslog(LOG_INFO, fmt)
2018-08-19 11:45:46 +02:00
/**
** Show debug.
*/
#ifdef DEBUG
#define Debug(level, fmt...) Syslog(level, fmt)
2018-08-19 11:45:46 +02:00
#else
#define Debug(level, fmt...) /* disabled */
2018-08-19 11:45:46 +02:00
#endif
#ifndef AV_NOPTS_VALUE
#define AV_NOPTS_VALUE INT64_C(0x8000000000000000)
#endif
/**
** Nice time-stamp string.
**
** @param ts dvb time stamp
*/
static inline const char *Timestamp2String(int64_t ts) {
2018-08-19 11:45:46 +02:00
static char buf[4][16];
static int idx;
if (ts == (int64_t)AV_NOPTS_VALUE) {
return "--:--:--.---";
2018-08-19 11:45:46 +02:00
}
idx = (idx + 1) % 3;
snprintf(buf[idx], sizeof(buf[idx]), "%2d:%02d:%02d.%03d", (int)(ts / (90 * 3600000)),
(int)((ts / (90 * 60000)) % 60), (int)((ts / (90 * 1000)) % 60), (int)((ts / 90) % 1000));
2018-08-19 11:45:46 +02:00
return buf[idx];
}
/**
** Get ticks in ms.
**
** @returns ticks in ms,
*/
static inline uint32_t GetMsTicks(void) {
2018-08-19 11:45:46 +02:00
#ifdef CLOCK_MONOTONIC
struct timespec tspec;
clock_gettime(CLOCK_MONOTONIC, &tspec);
return (tspec.tv_sec * 1000) + (tspec.tv_nsec / (1000 * 1000));
#else
struct timeval tval;
if (gettimeofday(&tval, NULL) < 0) {
return 0;
2018-08-19 11:45:46 +02:00
}
return (tval.tv_sec * 1000) + (tval.tv_usec / 1000);
#endif
}
static inline uint64_t GetusTicks(void) {
2018-08-19 11:45:46 +02:00
#ifdef CLOCK_MONOTONIC
struct timespec tspec;
clock_gettime(CLOCK_MONOTONIC, &tspec);
return (uint64_t)(tspec.tv_sec * 1000000) + (tspec.tv_nsec);
2018-08-19 11:45:46 +02:00
#else
struct timeval tval;
if (gettimeofday(&tval, NULL) < 0) {
return 0;
2018-08-19 11:45:46 +02:00
}
return (tval.tv_sec * 1000) + (tval.tv_usec / 1000);
#endif
}
/// @}