Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / eventlogwriter.pl @ e26d3d25

History | View | Annotate | Download (6.03 KB)

1 01873262 Georg Kunz
#
2
# Generates eventlog writer code from eventlogentries.txt
3
#
4
# Author: Andras Varga, 2006
5
#
6
7
#FIXME: make use of "default value" column!!
8
9
$verbose = 0;
10
11
open(FILE, "../eventlog/eventlogentries.txt");
12
13
14
#
15
# Read input file
16
#
17
while (<FILE>)
18
{
19
   chomp;
20
   s/\r$//; # cygwin/mingw perl does not do CR/LF translation
21
22
   if ($_ =~ /^ *$/)
23
   {
24
      # blank line
25
   }
26
   elsif ($_ =~ /^ *\/\//)
27
   {
28
      # comment
29
   }
30
   elsif ($_ =~ /^([\w]+) +([\w]+) *(\/\/.*)?$/)
31
   {
32
      $classCode = $1;
33
      $className = $2;
34
      $classHasOptField = 0;
35
      print "$classCode $className\n" if ($verbose);
36
   }
37
   elsif ($_ =~ /^ *{ *$/)
38
   {
39
      print "{\n" if ($verbose);
40
   }
41
   elsif ($_ =~ /^ +([\w#]+) +([\w]+) +([\w]+)( +([^ ]+))? *(\/\/.*)?$/)
42
   {
43
      $fieldCode = $1;
44
      $fieldType = $2;
45
      $fieldName = $3;
46
      $fieldDefault  = $5;
47
      $fieldPrintfValue = $fieldName;
48
49
      if ($fieldType eq "string")
50
      {
51
         $fieldPrintfType = "%s";
52
         $fieldPrintfValue = "QUOTE($fieldPrintfValue)";
53
      }
54
      elsif ($fieldType eq "bool")
55
      {
56
         $fieldPrintfType = "%d";
57
      }
58
      elsif ($fieldType eq "int")
59
      {
60
         $fieldPrintfType = "%d";
61
      }
62
      elsif ($fieldType eq "short")
63
      {
64
         $fieldPrintfType = "%d";
65
      }
66
      elsif ($fieldType eq "long")
67
      {
68
         $fieldPrintfType = "%ld";
69
      }
70
      elsif ($fieldType eq "int64")
71
      {
72
         $fieldPrintfType = '%"LL"d';
73
      }
74
      elsif ($fieldType eq "eventnumber_t")
75
      {
76
         $fieldPrintfType = '%"LL"d';
77
      }
78
      elsif ($fieldType eq "simtime_t")
79
      {
80
         $fieldPrintfType = "%s";
81
         $fieldPrintfValue = "SIMTIME_STR($fieldPrintfValue)";
82
      }
83
      else {
84
         die "unrecognized type '$fieldType'";
85
      }
86
87
      if ($fieldDefault ne "") {
88
         $classHasOptField = 1;
89
      }
90
91
      $fieldCType = $fieldType;
92
      $fieldCType =~ s/string/const char */;
93
      $field = {
94
         CODE => $fieldCode,
95
         TYPE => $fieldType,
96
         CTYPE => $fieldCType,
97
         PRINTFTYPE => $fieldPrintfType,
98
         PRINTFVALUE => $fieldPrintfValue,
99
         NAME => $fieldName,
100
         DEFAULTVALUE => $fieldDefault,
101
      };
102
103
      push(@fields, $field);
104
      print " $fieldCode $fieldType $fieldName $fieldDefault\n" if ($verbose);
105
   }
106
   elsif ($_ =~ /^ *} *$/)
107
   {
108
      $class = {
109
         CODE => $classCode,
110
         NAME => $className,
111
         HASOPT => $classHasOptField,
112
         FIELDS => [ @fields ],
113
      };
114
      push(@classes, $class);
115
      @fields = ();
116
      print "}\n" if ($verbose);
117
   }
118
   else
119
   {
120
       die "unrecognized line \"$_\"";
121
   }
122
}
123
124
close(FILE);
125
126
127
128
#
129
# Write eventlogwriter.h file
130
#
131
132
open(H, ">eventlogwriter.h");
133
134
print H makeFileBanner("eventlogwriter.h");
135
print H
136
"#ifndef __EVENTLOGWRITER_H_
137
#define __EVENTLOGWRITER_H_
138
139
#include <stdio.h>
140
#include \"envirdefs.h\"
141
#include \"simtime_t.h\"
142
143
USING_NAMESPACE
144
145
class EventLogWriter
146
{
147
  public:
148
    static void recordLogLine(FILE *f, const char *s, int n);
149
";
150
151
foreach $class (@classes)
152
{
153
   print H "    static void " . makeMethodDecl($class,0) . ";\n";
154
   print H "    static void " . makeMethodDecl($class,1) . ";\n" if ($class->{HASOPT});
155
}
156
157
print H "};
158
159
#endif
160
";
161
162
close(H);
163
164
165
166
#
167
# Write eventlogwriter.cc file
168
#
169
170
open(CC, ">eventlogwriter.cc");
171
172
print CC makeFileBanner("eventlogwriter.cc");
173
print CC "
174
#include \"eventlogwriter.h\"
175
#include \"stringutil.h\"
176
#include \"cconfigoption.h\"
177
#include \"csimulation.h\"
178
#include \"cmodule.h\"
179
#include \"cexception.h\"
180
181
#ifdef CHECK
182
#undef CHECK
183
#endif
184
#define CHECK(fprintf)    if (fprintf<0) throw cRuntimeError(\"Cannot write event log file, disk full?\");
185
186
#define LL    INT64_PRINTF_FORMAT
187
188
void EventLogWriter::recordLogLine(FILE *f, const char *s, int n)
189
{
190
    CHECK(fprintf(f, \"- \"));
191
    CHECK(fwrite(s, 1, n, f));
192
}
193
194
";
195
196
foreach $class (@classes)
197
{
198
   print CC makeMethodImpl($class,0);
199
   print CC makeMethodImpl($class,1) if ($class->{HASOPT});
200
}
201
202
close(CC);
203
204
sub makeMethodImpl ()
205
{
206
   my $class = shift;
207
   my $wantOptFields = shift;
208
209
   my $txt = "void EventLogWriter::" . makeMethodDecl($class,$wantOptFields) . "\n{\n";
210
211
   # class code goes into initial fprintf
212
   my $fmt .= "$class->{CODE}";
213
   $fmt = "\\n".$fmt if ($class->{CODE} eq "E");
214
   my $args = "";
215
216
   foreach $field (@{ $class->{FIELDS} })
217
   {
218
      # if wantOptFields==false, skip optional fields
219
      next if (!$wantOptFields && $field->{DEFAULTVALUE} ne "");
220
221
      if ($field->{DEFAULTVALUE} eq "")
222
      {
223
         # mandatory field: append to current fprintf statement
224
         $fmt .= " $field->{CODE} $field->{PRINTFTYPE}";
225
         $args .= ", $field->{PRINTFVALUE}";
226
      }
227
      else
228
      {
229
         # optional field: flush current fprintf statement, and generate a conditional fprintf
230
         $txt .= "    CHECK(fprintf(f, \"$fmt\"$args));\n" if ($fmt ne "");
231
         $fmt = "";
232
         $args = "";
233
         $txt .= "    if ($field->{NAME}!=$field->{DEFAULTVALUE})\n";
234
         $txt .= "        CHECK(fprintf(f, \" $field->{CODE} $field->{PRINTFTYPE}\", $field->{PRINTFVALUE}));\n";
235
      }
236
   }
237
   # flush final fprintf statement (or at least a newline if $fmt=="")
238
   $txt .= "    CHECK(fprintf(f, \"$fmt\\n\"$args));\n";
239
240
   $txt .= "}\n\n";
241
   $txt;
242
}
243
244
sub makeMethodDecl ()
245
{
246
   my $class = shift;
247
   my $wantOptFields = shift;
248
249
   my $txt = "record$class->{NAME}";
250
   foreach $field (@{ $class->{FIELDS} })
251
   {
252
      my $code = ($field->{CODE} eq "#") ? "e" : $field->{CODE};
253
      $txt .= "_$code" if ($wantOptFields || $field->{DEFAULTVALUE} eq "");
254
   }
255
   $txt .= "(FILE *f";
256
   foreach $field (@{ $class->{FIELDS} })
257
   {
258
      $txt .= ", $field->{CTYPE} $field->{NAME}" if ($wantOptFields || $field->{DEFAULTVALUE} eq "");
259
   }
260
   $txt .= ")";
261
   $txt;
262
}
263
264
sub makeFileBanner ()
265
{
266
    my $ucfilename = uc(shift);
267
    return
268
"//=========================================================================
269
// $ucfilename - part of
270
//                  OMNeT++/OMNEST
271
//           Discrete System Simulation in C++
272
//
273
//  This is a generated file -- do not modify.
274
//
275
//=========================================================================
276
277
";
278
}
279