Convert %lld and %llu in printf formats to G_G[U]INT64_FORMAT. Fix pointer<->int...
[platform/upstream/gstreamer.git] / gst / gsttrace.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gsttrace.c: Tracing functions (depracated)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <string.h>
30
31 #include "gst_private.h"
32
33 #include "gstlog.h"
34 #include "gsttrace.h"
35
36 static 
37 #ifdef __inline__
38 __inline__
39 #endif
40 void
41 read_tsc (gint64 * dst)
42 {
43 #ifdef HAVE_RDTSC
44   guint64 tsc;
45   __asm__ __volatile__ ("rdtsc":"=A" (tsc));
46
47   *dst = tsc;
48 #else
49   *dst = 0;
50 #endif
51 }
52
53 void
54 gst_trace_read_tsc (gint64 * dst)
55 {
56   read_tsc (dst);
57 }
58
59 GstTrace *_gst_trace_default = NULL;
60 gint _gst_trace_on = 1;
61
62 GstTrace *
63 gst_trace_new (gchar * filename, gint size)
64 {
65   GstTrace *trace = g_malloc (sizeof (GstTrace));
66
67   g_return_val_if_fail (trace != NULL, NULL);
68   trace->filename = g_strdup (filename);
69   g_print ("opening '%s'\n", trace->filename);
70   trace->fd = open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
71   perror ("opening trace file");
72   g_return_val_if_fail (trace->fd > 0, NULL);
73   trace->buf = g_malloc (size * sizeof (GstTraceEntry));
74   g_return_val_if_fail (trace->buf != NULL, NULL);
75   trace->bufsize = size;
76   trace->bufoffset = 0;
77
78   return trace;
79 }
80
81 void
82 gst_trace_destroy (GstTrace * trace)
83 {
84   g_return_if_fail (trace != NULL);
85   g_return_if_fail (trace->buf != NULL);
86
87   if (gst_trace_get_remaining (trace) > 0)
88     gst_trace_flush (trace);
89   close (trace->fd);
90   g_free (trace->buf);
91   g_free (trace);
92 }
93
94 void
95 gst_trace_flush (GstTrace * trace)
96 {
97   if (!trace) {
98     trace = _gst_trace_default;
99     if (!trace)
100       return;
101   }
102
103   write (trace->fd, trace->buf, trace->bufoffset * sizeof (GstTraceEntry));
104   trace->bufoffset = 0;
105 }
106
107 void
108 gst_trace_text_flush (GstTrace * trace)
109 {
110   int i;
111   const int strsize = 20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1;
112   char str[strsize];
113
114   if (!trace) {
115     trace = _gst_trace_default;
116     if (!trace)
117       return;
118   }
119
120   for (i = 0; i < trace->bufoffset; i++) {
121     snprintf (str, strsize, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
122               trace->buf[i].timestamp,
123               trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
124     write (trace->fd, str, strlen (str));
125   }
126   trace->bufoffset = 0;
127 }
128
129 void
130 gst_trace_set_default (GstTrace * trace)
131 {
132   g_return_if_fail (trace != NULL);
133   _gst_trace_default = trace;
134 }
135
136 void
137 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
138 {
139   GstTraceEntry *entry;
140
141   if (!trace) {
142     trace = _gst_trace_default;
143     if (!trace)
144       return;
145   }
146
147   entry = trace->buf + trace->bufoffset;
148   read_tsc (&(entry->timestamp));
149   entry->sequence = seq;
150   entry->data = data;
151   strncpy (entry->message, msg, 112);
152   trace->bufoffset++;
153
154   gst_trace_flush (trace);
155 }