1 // Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stdlib.h>
6 #include <unistd.h>
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/logging.h"
12 
13 #include "compat/log_level.h"
14 #include "compat/string.h"
15 #include "conversion_utils.h"
16 
17 using quipper::FormatAndFile;
18 using quipper::kPerfFormat;
19 using quipper::kProtoTextFormat;
20 
21 namespace {
22 // Default output format of this tool is proto text format.
23 const char kDefaultOutputFormat[] = "text";
24 
25 // Default output filename is simply stdout.
26 const char kDefaultOutputFilename[] = "/dev/stdout";
27 
28 // Default input filename is perf.data in cwd.
29 const char kDefaultInputFilename[] = "perf.data";
30 
31 // Default input format is perf.data format.
32 const char kDefaultInputFormat[] = "perf";
33 
34 // Parses arguments, storing the results in |input| and |output|. Returns true
35 // if arguments parsed successfully and false otherwise.
ParseArguments(int argc,char * argv[],FormatAndFile * input,FormatAndFile * output)36 bool ParseArguments(int argc, char* argv[], FormatAndFile* input,
37                     FormatAndFile* output) {
38   output->filename = kDefaultOutputFilename;
39   output->format = kDefaultOutputFormat;
40   input->filename = kDefaultInputFilename;
41   input->format = kDefaultInputFormat;
42 
43   int opt;
44   while ((opt = getopt(argc, argv, "i:o:I:O:v:")) != -1) {
45     switch (opt) {
46       case 'i': {
47         input->filename = optarg;
48         break;
49       }
50       case 'I': {
51         input->format = optarg;
52         break;
53       }
54       case 'o': {
55         output->filename = optarg;
56         break;
57       }
58       case 'O': {
59         output->format = optarg;
60         break;
61       }
62       case 'v': {
63         quipper::SetVerbosityLevel(atoi(optarg));
64         break;
65       }
66       default:
67         return false;
68     }
69   }
70   return true;
71 }
72 
PrintUsage()73 void PrintUsage() {
74   LOG(INFO) << "Usage:";
75   LOG(INFO) << "<exe> -i <input filename> -I <input format>"
76             << " -o <output filename> -O <output format> -v <verbosity level>";
77   LOG(INFO) << "Format options are: '" << kPerfFormat << "' for perf.data"
78             << " and '" << kProtoTextFormat << "' for proto text.";
79   LOG(INFO) << "By default it reads from perf.data and outputs to /dev/stdout"
80             << " in proto text format.";
81   LOG(INFO) << "Default verbosity level is 0. Higher values increase verbosity."
82             << " Negative values filter LOG() levels.";
83 }
84 }  // namespace
85 
main(int argc,char * argv[])86 int main(int argc, char* argv[]) {
87   FormatAndFile input, output;
88   if (!ParseArguments(argc, argv, &input, &output)) {
89     PrintUsage();
90     return EXIT_FAILURE;
91   }
92 
93   if (!quipper::ConvertFile(input, output)) return EXIT_FAILURE;
94   return EXIT_SUCCESS;
95 }
96