Przeglądaj źródła

redirect output

develop
Robin Thoni 7 lat temu
rodzic
commit
f19e818ce4
1 zmienionych plików z 30 dodań i 9 usunięć
  1. 30
    9
      cli/Interface/MainClass.cpp

+ 30
- 9
cli/Interface/MainClass.cpp Wyświetl plik

@@ -13,12 +13,13 @@
13 13
 #include "CommandLineParser.h"
14 14
 #include "MainClass.h"
15 15
 
16
-#define EX_KEY_ERROR 1
17
-#define EX_LIB_NFC_ERROR 2
18
-#define EX_NFC_DEVICE_NOT_FOUND 3
19
-#define EX_NFC_TAG_NOT_FOUND 4
20
-#define EX_MAP_KEYS_ERROR 5
21
-#define EX_DUMP_ERROR 6
16
+#define EX_REDIRECT_ERROR 1
17
+#define EX_KEY_ERROR 10
18
+#define EX_LIB_NFC_ERROR 12
19
+#define EX_NFC_DEVICE_NOT_FOUND 13
20
+#define EX_NFC_TAG_NOT_FOUND 14
21
+#define EX_MAP_KEYS_ERROR 15
22
+#define EX_DUMP_ERROR 16
22 23
 
23 24
 MainClass::MainClass(int argc, char *argv[])
24 25
     : _argc(argc)
@@ -43,9 +44,26 @@ int MainClass::main()
43 44
     CommandLineOption optionKeyFile(&parser, "key-file", 'f', "Path to a file containing keys", "FILE");
44 45
     CommandLineOption optionKey(&parser, "key", 'k', "Key to use to authenticate", "KEY");
45 46
 
47
+    CommandLineOption optionOutput(&parser, "output", 'o', "Redirect output to FILE. '-' to use stdout", "FILE");
48
+
46 49
     if (!parser.parse()) {
47 50
         return parser.showHelp(EX_USAGE);
48 51
     }
52
+    std::string outputFile = "-";
53
+    if (optionOutput.isSet()) {
54
+        outputFile = optionOutput.getValue();
55
+    }
56
+    std::streambuf* oldCoutStreamBuf = std::cout.rdbuf();
57
+    std::ofstream fileCout;
58
+    if (outputFile != "-" && !outputFile.empty()) {
59
+        fileCout.open(outputFile);
60
+        if (!fileCout) {
61
+            std::cerr << "Failed to redirect output: " << strerror(errno) << std::endl;
62
+            return EX_REDIRECT_ERROR;
63
+        }
64
+        std::cout.rdbuf(fileCout.rdbuf());
65
+    }
66
+
49 67
     if (optionVersion.isSet()) {
50 68
         printVersion();
51 69
         return EX_OK;
@@ -159,20 +177,23 @@ int MainClass::main()
159 177
                                     res = mapKeys(tag, keys);
160 178
                                 }
161 179
                                 else {
162
-                                    std::cerr << "Must select an action (dump|map|devices|tags)" << std::endl;
180
+                                    std::cerr << "Must select an action (map|dump|devices|tags)" << std::endl;
163 181
                                     res = EX_USAGE;
164 182
                                 }
165 183
                             }
166 184
                         }
167 185
                     }
186
+                    device->close();
168 187
                 }
169
-                device->close();
170 188
             }
171 189
         }
172 190
     }
173 191
 
174 192
     libNfc.clean();
175
-
193
+    std::cout.rdbuf(oldCoutStreamBuf);
194
+    if (fileCout) {
195
+        fileCout.close();
196
+    }
176 197
     return res;
177 198
 }
178 199
 

Ładowanie…
Anuluj
Zapisz