summaryrefslogtreecommitdiff
path: root/src/bin/logfiles.c
blob: bbe69def325eee516ea87ae6110ae7b1ee0cc773 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright © 2011 ASP64 <guillaume.friloux@asp64.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * @file logfiles.c
 * @brief Manages log files list
 * @author Guillaume Friloux <guillaume.friloux@asp64.com>
 * @version 1.0
 *
 * Manages log files list
 * @see logfile
 * @see list_logfiles
 */
#include "logfiles.h"

/**
 * @fn int logfiles_exist(char *logfile)
 * @brief Checks in the list of log files we are watching if we
 *  already watch a given logfile
 *
 * @param logfile (char *) Name of the logfile to check
 *
 * @return 0 if not found, 1 if found
 */
int logfiles_exist(char *logfile)
{
	Eina_List *l;
	struct logfile *foundlogfile = NULL;
	EINA_LIST_FOREACH(list_logfiles, l, foundlogfile)
	{
		if( !strcmp(foundlogfile->name, logfile) )
		{
			return(1);
		}
	}

	return(0);
}

/**
 * @fn int logfiles_add(struct logfile *new_logfile)
 * @brief Adds a logfile struct inside the logfiles list
 *
 * @param new_logfile (struct logfile *) structure to add
 *
 * @return 0 if not found, 1 if found
 */
int logfiles_add(struct logfile *new_logfile)
{
	list_logfiles = eina_list_append(list_logfiles, new_logfile);
	return(0);
}

/**
 * @fn int logfiles_print(void)
 * @brief Prints on stdout the list of logfiles. Only usefull for a bit of
 *  monitoring of what is going on
 *
 * @return 0 if not found, 1 if found
 */
int logfiles_print(void)
{
	Eina_List *l;
	struct logfile *foundlogfile = NULL;
	EINA_LIST_FOREACH(list_logfiles, l, foundlogfile)
	{
		EINA_LOG_DOM_INFO(einadom_logfiles, "Name = %s", foundlogfile->name);
	}
	return(0);
}

/**
 * @fn int logfiles_new(struct logfile **new_logfile, char *filename, fpos_t cursor, unsigned long long int filesize)
 * @brief Will alloc a new logfile structure and set given values
 *
 * @param new_logfile (struct logfile *) structure to alloc
 * @param filename (char *) Name of the logfile
 * @param cursor (fpos_t) Position of the cursor
 * @param filesize (unsigned long long int) Size of the file
 *
 * @return 0
 */
int logfiles_new(struct logfile **new_logfile, char *filename, fpos_t cursor, unsigned long long int filesize)
{
	*new_logfile = malloc(sizeof(struct logfile));
	(*new_logfile)->name = malloc(sizeof(char) * ( strlen(filename) + 1 ) );
	strcpy( (*new_logfile)->name, filename);
	(*new_logfile)->cursor = cursor;
	(*new_logfile)->filesize = filesize;

	return(0);
}

/**
 * @fn int logfiles_del(struct logfile **old_logfile)
 * @brief This function will free an allocated logfile structure
 *
 * @param old_logfile (struct logfile **) structure to free
 *
 * @return 0
 */
int logfiles_del(struct logfile **old_logfile)
{
	if( !(*old_logfile) )
		return(0);

	if( (*old_logfile)->name )
		free((*old_logfile)->name);

	free((*old_logfile));
	return(0);
}

/**
 * @fn int logfiles_getend(char *logfile, fpos_t *pos_end)
 * @brief This function will get a cursor to the end of the file
 *
 * @param logfile (char *) logfile involved
 * @param pos_end (fpos_t *) pointer used to store the position
 *
 * @return 0 or -1 if an error occur
 */
int logfiles_getend(char *logfile, fpos_t *pos_end)
{
	FILE *fp;

	// We get our cursor pos
	fp = fopen(logfile, "r");
	if( !fp )
	{
		EINA_LOG_DOM_ERR(einadom_logfiles, "Can't open %s : %s", logfile, strerror(errno));
		return(-1);
	}
	fseeko(fp, 0L, SEEK_END);
	fgetpos(fp, pos_end);

	fclose(fp);
	return(0);
}

/**
 * @fn int logfiles_getbegin(char *logfile, fpos_t *pos_begin)
 * @brief This function will get a cursor to the begin of the file
 *
 * @param logfile (char *) logfile involved
 * @param pos_begin (fpos_t *) pointer used to store the position
 *
 * @return 0 or -1 if an error occur
 */
int logfiles_getbegin(char *logfile, fpos_t *pos_begin)
{
	FILE *fp;

	// We get our cursor pos
	fp = fopen(logfile, "r");
	if( !fp )
	{
		EINA_LOG_DOM_ERR(einadom_logfiles, "Can't open %s : %s", logfile, strerror(errno));
		return(-1);
	}
	fgetpos(fp, pos_begin);

	fclose(fp);
	return(0);
}

/**
 * @fn int logfiles_getsize(char *logfile, unsigned long long int *filesize)
 * @brief This function will get the size of a file
 *
 * @param logfile (char *) logfile involved
 * @param filesize (unsigned long long int) pointer used to store the size
 *
 * @return 0 or -1 if an error occur
 */
int logfiles_getsize(char *logfile, unsigned long long int *filesize)
{
	struct stat64 st_buffer;
	int retour;

	retour = stat64(logfile, &st_buffer);

	if( retour )
	{
		EINA_LOG_DOM_ERR(einadom_logfiles, "Can't get size of %s : %s", logfile, strerror(errno));
		return(-1);
	}

	*filesize = st_buffer.st_size;

	return(0);
}