The Assimilation Project  based on Assimilation version 1.1.7.1474836767
logsourcefd.c
Go to the documentation of this file.
1 
24 #include <projectcommon.h>
25 #include <gmainfd.h>
26 #include <logsourcefd.h>
27 #include <string.h>
28 
34 FSTATIC void logsourcefd_newtext(GMainFd*, const char *, int len);
36 
39 logsourcefd_new(gsize cpsize
40 , int fd
41 , int priority
42 , GMainContext* context
43 , const char * logdomain
44 , GLogLevelFlags loglevel
45 , const char * prefix)
46 
47 {
48  GMainFd* fdself;
49  LogSourceFd* self;
50 
51  if (cpsize < sizeof(LogSourceFd)) {
52  cpsize = sizeof(LogSourceFd);
53  }
54  fdself = gmainfd_new(cpsize, fd, priority, context);
55  g_return_val_if_fail(fdself != NULL, NULL);
56  self = NEWSUBCLASS(LogSourceFd, fdself);
57 
58  fdself->newtext = logsourcefd_newtext;
60  self->logdomain = g_strdup(logdomain);
61  self->prefix = g_strdup(prefix);
62  self->loglevel = loglevel;
63  return self;
64 }
65 
67 FSTATIC void
68 logsourcefd_newtext(GMainFd* fdself, const char * string, int len)
69 {
70  GString* thisline = NULL;
71  LogSourceFd* self = CASTTOCLASS(LogSourceFd, fdself);
72  const int prefixlen = strlen(self->prefix);
73  int j;
74 
75  self->charcount += len;
76  for (j=0; j < len; ++j) {
77  if (string[j] == '\n') {
78  self->linecount += 1;
79  if (thisline) {
80  g_log(self->logdomain, self->loglevel, "%s%s", self->prefix, thisline->str);
81  g_string_free(thisline, TRUE);
82  thisline = NULL;
83  }
84  continue;
85  }
86  if (thisline == NULL) {
87  thisline = g_string_sized_new(prefixlen + (len-j));
88  thisline = g_string_append_c(thisline, string[j]);
89  }else{
90  thisline = g_string_append_c(thisline, string[j]);
91  }
92  }
93  if (thisline) {
94  g_log(self->logdomain, self->loglevel, "%s%s", self->prefix, thisline->str);
95  g_string_free(thisline, TRUE);
96  thisline = NULL;
97  }
98 }
99 
100 
101 
103 FSTATIC void
105 {
106  LogSourceFd* self = CASTTOCLASS(LogSourceFd, fdself);
107  if (self->logdomain) {
108  g_free(self->logdomain);
109  self->logdomain = NULL;
110  }
111  if (self->prefix) {
112  g_free(self->prefix);
113  self->prefix = NULL;
114  }
115 }
void(* newtext)(GMainFd *, const char *, int)
Deal with newly read text.
Definition: gmainfd.h:40
FSTATIC void logsourcefd_finalize(GMainFd *fdself)
LogSourceFd class. finalize routine
Definition: logsourcefd.c:104
#define FSTATIC
Definition: projectcommon.h:31
LogSourceFd * logsourcefd_new(gsize cpsize, int fd, int priority, GMainContext *context, const char *logdomain, GLogLevelFlags loglevel, const char *prefix)
Construct a new LogSourceFd class. object and return it.
Definition: logsourcefd.c:39
void(* finalize)(GMainFd *)
finalize function
Definition: gmainfd.h:42
Project common header file.
FSTATIC void logsourcefd_newtext(GMainFd *, const char *, int len)
Just stash away our new string - appending to what&#39;s already there.
Definition: logsourcefd.c:68
Implements a gmainloop source for reading file descriptor pipes.
GMainFd * gmainfd_new(gsize cpsize, int fd, int priority, GMainContext *context)
Construct a new GMainFd object and return it.
Definition: gmainfd.c:64
#define CASTTOCLASS(Cclass, obj)
Safely cast &#39;obj&#39; to C-class &#39;class&#39; - verifying that it was registerd as being of type class ...
Definition: proj_classes.h:66
Implements a gmainloop source for reading file descriptor pipes.
#define NEWSUBCLASS(Cclass, obj)
Definition: proj_classes.h:67
struct _LogSourceFd LogSourceFd
Definition: logsourcefd.h:32