summaryrefslogtreecommitdiff
path: root/common.c
blob: 17b594bc9ccb5c85380513809d236d2630394b6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * common.c: EPG2VDR plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <sys/time.h>
#include <stdarg.h>
#include <string.h>
#include <syslog.h>

#include <vdr/thread.h>

#include "common.h"
#include "config.h"

cMutex logMutex;

//***************************************************************************
// Tell
//***************************************************************************

void tell(int eloquence, const char* format, ...)
{
   if (cfg.loglevel < eloquence)
      return ;

   const int sizeBuffer = 100000;
   char t[sizeBuffer+100]; *t = 0;
   va_list ap;

   cMutexLock lock(&logMutex);
   
   va_start(ap, format);

   snprintf(t, sizeBuffer, "SEDUATMO: ");
   vsnprintf(t+strlen(t), sizeBuffer-strlen(t), format, ap);
   
   syslog(LOG_ERR, "%s", t);

   va_end(ap);
}

//***************************************************************************
// Error
//***************************************************************************

int error(const char* format, ...)
{
   const int sizeBuffer = 100000;
   char t[sizeBuffer+100]; *t = 0;
   va_list ap;

   cMutexLock lock(&logMutex);
   
   va_start(ap, format);

   snprintf(t, sizeBuffer, "SEDUATMO: ");
   vsnprintf(t+strlen(t), sizeBuffer-strlen(t), format, ap);
   
   syslog(LOG_ERR, "%s", t);

   va_end(ap);

   return fail;
}

//***************************************************************************
// msNow
//***************************************************************************

MsTime msNow()
{
   timeval tv;

   gettimeofday(&tv, 0); 

   return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

//***************************************************************************
// Misc. Functions
//***************************************************************************

int minMax(int x, int min, int max)
{
   if (x < min)
      return min;
   
   if (max < x)
      return max;
   
   return x;
}

double min(double a, double b)
{
    return a < b ? a : b;
}

double max(double a, double b)
{
    return a >= b ? a : b;
}

int getrand(int min, int max)
{
    srand(time(0));
    return rand() % (max-min) + min;
}