aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/libraries/LumbrJack.c
blob: 9b072b04ec1bd0bbb3548171c1e9779f11a6968e (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*  LumbrJack - a logging library that wraps Eina logging.

*/


#include <unistd.h>
#include "LumbrJack.h"


static char dateTime[DATE_TIME_LEN];
static Eina_Prefix *prefix = NULL;


int HamrTime(char *argv0, void *main, int logDom)
{
  Eina_Array *path;
  char *env, name[PATH_MAX], cwd[PATH_MAX], temp[PATH_MAX * 2];
  int i, len;

  if (!eina_init())
  {
    printf("Can't load eina library, nothing else will work!\n");
    exit(0);
  }

  // Coz eina_file_split splits the string in place, instead of making a copy first.
  snprintf(temp, sizeof(temp), "%s", argv0);
  path = eina_file_split(temp);
  snprintf(name, sizeof(name), "%s", (char *) eina_array_data_get(path, eina_array_count(path) - 1));
  logDom = loggingStartup(name, logDom);
  eina_array_free(path);

  len = strlen(name);
  temp[len] = 0;
  cwd[len]  = 0;
  for (i = 0;  i < len;  i++)
  {
    temp[i] = toupper(name[i]);
    cwd[i]  = tolower(name[i]);
  }

  if (!(prefix = eina_prefix_new(argv0, main, temp, cwd, "checkme.txt", PACKAGE_BIN_DIR, PACKAGE_LIB_DIR, PACKAGE_DATA_DIR, PACKAGE_LOCALE_DIR)))
    PC("Can't find application prefix!");

  PD("%s is installed in %s", name, eina_prefix_get(prefix));
  PD("The binaries are in %s", eina_prefix_bin_get(prefix));
  PD("The data files are in %s", eina_prefix_data_get(prefix));
  PD("The libraries are in %s", eina_prefix_lib_get(prefix));
  PD("The locale files are in %s", eina_prefix_locale_get(prefix));

  getcwd(cwd, PATH_MAX);
  env = getenv("LUA_CPATH");
  if (!env)  env = "";
  sprintf(temp, "%s;%s/lib?.so;%s/?.so;%s/?.so", env, eina_prefix_lib_get(prefix), eina_prefix_lib_get(prefix), cwd);
  setenv("LUA_CPATH", temp, 1);

  env = getenv("LUA_PATH");
  if (!env)  env = "";
  sprintf(temp, "%s;%s/?.lua;%s/?.lua", env, eina_prefix_lib_get(prefix), cwd);
  setenv("LUA_PATH", temp, 1);

  return logDom;
}

const char *prefix_get()		{return eina_prefix_get(prefix);}
const char *prefix_bin_get()		{return eina_prefix_bin_get(prefix);}
const char *prefix_data_get()		{return eina_prefix_data_get(prefix);}
const char *prefix_lib_get()		{return eina_prefix_lib_get(prefix);}
const char *prefix_locale_get()		{return eina_prefix_locale_get(prefix);}

void pantsOff(int logDom)
{
  if (logDom >= 0)
    eina_log_domain_unregister(logDom);

  eina_prefix_free(prefix);

  eina_shutdown();
}

static void _logPrint(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args)
{
  FILE *f = data;
  char dt[DATE_TIME_LEN + 1];
  char fileTab[256], funcTab[256];

  getDateTime(NULL, dt, NULL);
  dt[19] = '\0';
  if (12 > strlen(file))
    snprintf(fileTab, sizeof(fileTab), "%s\t\t", file);
  else
    snprintf(fileTab, sizeof(fileTab), "%s\t", file);
  snprintf(funcTab, sizeof(funcTab), "\t%s", fnc);
  fprintf(f, "%s ", dt);
  if (f == stderr)
    eina_log_print_cb_stderr(d, level, fileTab, funcTab, line, fmt, data, args);
  else if (f == stdout)
    eina_log_print_cb_stdout(d, level, fileTab, funcTab, line, fmt, data, args);
  fflush(f);
}

int loggingStartup(char *name, int logDom)
{
  eina_log_threads_enable();

  if (logDom < 0)
  {
    logDom = eina_log_domain_register(name, EINA_COLOR_ORANGE);
    if (logDom < 0)
    {
      EINA_LOG_CRIT("could not register log domain '%s'", name);
      return logDom;
    }
  }
  eina_log_level_set(EINA_LOG_LEVEL_DBG);
  eina_log_domain_level_set(name, EINA_LOG_LEVEL_DBG);
  eina_log_print_cb_set(_logPrint, stderr);

  // Shut up the excess debugging shit from EFL.
  eina_log_domain_level_set("eo", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("eldbus", EINA_LOG_LEVEL_CRITICAL);
  eina_log_domain_level_set("eet", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("efreet_icon", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("ecore", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("ecore_audio", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("ecore_con", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("ecore_evas", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("ecore_input_evas", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("ecore_input_evas", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("ecore_system_upower", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("eio", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("ephysics", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("evas", EINA_LOG_LEVEL_WARN);
  eina_log_domain_level_set("evas_main", EINA_LOG_LEVEL_ERR);

  return logDom;
}

char *getDateTime(struct tm **nowOut, char *dateOut, time_t *timeOut)
{
  struct tm *newTime;
  time_t  szClock;
  char *date = dateTime;

  // Get time in seconds
  time(&szClock);
  // Convert time to struct tm form
  newTime = localtime(&szClock);

  if (nowOut)
    *nowOut = newTime;
  if (dateOut)
    date = dateOut;
  if (timeOut)
    *timeOut = szClock;

  // format
  strftime(date, DATE_TIME_LEN, "%Y-%m-%d %H:%M:%S\r", newTime);
  return (dateTime);
}

float timeDiff(struct timeval *now, struct timeval *then)
{
    if (0 == gettimeofday(now, 0))
    {
	struct timeval thisTime = { 0, 0 };
	double  result = 0.0;

	thisTime.tv_sec = now->tv_sec;
	thisTime.tv_usec = now->tv_usec;
	if (thisTime.tv_usec < then->tv_usec)
	{
	    thisTime.tv_sec--;
	    thisTime.tv_usec += 1000000;
	}
	thisTime.tv_usec -= then->tv_usec;
	thisTime.tv_sec -= then->tv_sec;
	result = ((double) thisTime.tv_usec) / ((double) 1000000.0);
	result += thisTime.tv_sec;
	return result;
    }
    else
	return 0.0;
}