Browse Source

redirect output

develop
Robin Thoni 7 years ago
parent
commit
f19e818ce4
1 changed files with 30 additions and 9 deletions
  1. 30
    9
      cli/Interface/MainClass.cpp

+ 30
- 9
cli/Interface/MainClass.cpp View File

13
 #include "CommandLineParser.h"
13
 #include "CommandLineParser.h"
14
 #include "MainClass.h"
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
 MainClass::MainClass(int argc, char *argv[])
24
 MainClass::MainClass(int argc, char *argv[])
24
     : _argc(argc)
25
     : _argc(argc)
43
     CommandLineOption optionKeyFile(&parser, "key-file", 'f', "Path to a file containing keys", "FILE");
44
     CommandLineOption optionKeyFile(&parser, "key-file", 'f', "Path to a file containing keys", "FILE");
44
     CommandLineOption optionKey(&parser, "key", 'k', "Key to use to authenticate", "KEY");
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
     if (!parser.parse()) {
49
     if (!parser.parse()) {
47
         return parser.showHelp(EX_USAGE);
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
     if (optionVersion.isSet()) {
67
     if (optionVersion.isSet()) {
50
         printVersion();
68
         printVersion();
51
         return EX_OK;
69
         return EX_OK;
159
                                     res = mapKeys(tag, keys);
177
                                     res = mapKeys(tag, keys);
160
                                 }
178
                                 }
161
                                 else {
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
                                     res = EX_USAGE;
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
     libNfc.clean();
192
     libNfc.clean();
175
-
193
+    std::cout.rdbuf(oldCoutStreamBuf);
194
+    if (fileCout) {
195
+        fileCout.close();
196
+    }
176
     return res;
197
     return res;
177
 }
198
 }
178
 
199
 

Loading…
Cancel
Save