summaryrefslogtreecommitdiff
path: root/lock-mgr/src/lockd-debug.c
blob: bff594986ab9bd92d30076e0b21f72b86733af6d (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
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.tizenopensource.org/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <glib.h>

#include "lockd-debug.h"

#define LINEMAX 256
#define MAXFILELEN	1048576
#define LOGFILE "/tmp/starter.log"

void lockd_log_t(char *fmt, ...)
{
	va_list ap;
	FILE *fd = 0;
	char buf[LINEMAX] = { 0, };
	char debugString[LINEMAX] = { 0, };

	va_start(ap, fmt);
	vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);
	int fileLen = 0;
	struct tm local_t;
	time_t current_time = 0;
	bzero((char *)&debugString, LINEMAX);
	time(&current_time);
	gmtime_r(&current_time, &local_t);
	int len = snprintf(debugString, sizeof(debugString),
			   "[%d-%02d-%02d, %02d:%02d:%02d]: ",
			   local_t.tm_year + 1900, local_t.tm_mon + 1,
			   local_t.tm_mday, local_t.tm_hour, local_t.tm_min,
			   local_t.tm_sec);
	if (len == -1) {
		return;
	} else {
		debugString[len] = '\0';
	}
	len = g_strlcat(debugString, buf, LINEMAX);
	if (len >= LINEMAX) {
		return;
	} else {
		debugString[len] = '\n';
	}
	if ((fd = fopen(LOGFILE, "at+")) == NULL) {
		LOCKD_DBG("File fopen fail for writing Pwlock information");
	} else {
		int pid = -1;
		if (fwrite(debugString, strlen(debugString), 1, fd) < 1) {
			LOCKD_DBG
			    ("File fwrite fail for writing Pwlock information");
			fclose(fd);
			if ((pid = fork()) < 0) {
			} else if (pid == 0) {
				execl("/bin/rm", "rm", "-f", LOGFILE,
				      (char *)0);
			}
		} else {
			fseek(fd, 0l, SEEK_END);
			fileLen = ftell(fd);
			if (fileLen > MAXFILELEN) {
				fclose(fd);
				if ((pid = fork()) < 0) {
					return;
				} else if (pid == 0) {
					execl("/bin/rm", "rm", "-f", LOGFILE,
					      (char *)0);
				}
			} else
				fclose(fd);
		}
	}
}