souffle  2.0.2-371-g6315b36
Cli.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2016, The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 #pragma once
10 
12 #include "souffle/profile/Tui.h"
13 
14 #include <iostream>
15 #include <map>
16 #include <string>
17 #include <vector>
18 #include <getopt.h>
19 
20 namespace souffle {
21 namespace profile {
22 
23 /*
24  * CLI to parse command line arguments and start up the TUI to either run a single command,
25  * generate the GUI file or run the TUI
26  */
27 class Cli {
28 public:
29  std::map<char, std::string> args;
30 
31  Cli(int argc, char* argv[]) : args() {
32  int c;
33  option longOptions[1];
34  longOptions[0] = {nullptr, 0, nullptr, 0};
35  while ((c = getopt_long(argc, argv, "c:hj::", longOptions, nullptr)) != EOF) {
36  // An invalid argument was given
37  if (c == '?') {
38  exit(EXIT_FAILURE);
39  }
40  if (optarg != nullptr) {
41  if (*optarg == '=') {
42  args[c] = optarg + 1;
43  } else {
44  args[c] = optarg;
45  }
46  } else {
47  args[c] = c;
48  }
49  }
50  if (optind < argc && args.count('f') == 0) {
51  args['f'] = argv[optind];
52  }
53  }
54 
55  void parse() {
56  if (args.size() == 0) {
57  std::cout << "No arguments provided.\nTry souffle-profile -h for help.\n";
58  exit(EXIT_FAILURE);
59  }
60 
61  if (args.count('h') != 0 || args.count('f') == 0) {
62  std::cout << "Souffle Profiler" << std::endl
63  << "Usage: souffle-profile <log-file> [ -h | -c <command> [options] | -j ]" << std::endl
64  << "<log-file> The log file to profile." << std::endl
65  << "-c <command> Run the given command on the log file, try with "
66  "'-c help' for a list"
67  << std::endl
68  << " of commands." << std::endl
69  << "-j[filename] Generate a GUI (html/js) version of the profiler."
70  << std::endl
71  << " Default filename is profiler_html/[num].html" << std::endl
72  << "-h Print this help message." << std::endl;
73  exit(0);
74  }
75  std::string filename = args['f'];
76 
77  if (args.count('c') != 0) {
78  Tui tui(filename, false, false);
79  for (auto& command : Tools::split(args['c'], ";")) {
80  tui.runCommand(Tools::split(command, " "));
81  }
82  } else if (args.count('j') != 0) {
83  if (args['j'] == "j") {
84  Tui(filename, false, true).outputHtml();
85  } else {
86  Tui(filename, false, true).outputHtml(args['j']);
87  }
88  } else {
89  Tui(filename, true, false).runProf();
90  }
91  }
92 };
93 
94 } // namespace profile
95 } // namespace souffle
souffle::profile::Tui::runProf
void runProf()
Definition: Tui.h:216
souffle::profile::Cli::Cli
Cli(int argc, char *argv[])
Definition: Cli.h:31
souffle::profile::Cli
Definition: Cli.h:27
souffle::profile::Tui
Definition: Tui.h:56
souffle::profile::Cli::args
std::map< char, std::string > args
Definition: Cli.h:29
souffle::profile::Tui::runCommand
void runCommand(std::vector< std::string > c)
Definition: Tui.h:125
souffle::profile::Tools::split
std::vector< std::string > split(std::string toSplit, std::string delimiter)
split on the delimiter
Definition: StringUtils.h:162
StringUtils.h
souffle::profile::Cli::parse
void parse()
Definition: Cli.h:55
souffle
Definition: AggregateOp.h:25
Tui.h