scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "DMJsonWriter.h" |
| 9 | |
| 10 | #include "SkCommonFlags.h" |
| 11 | #include "SkJSONCPP.h" |
| 12 | #include "SkOSFile.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkTArray.h" |
| 15 | #include "SkThread.h" |
| 16 | |
| 17 | namespace DM { |
| 18 | |
| 19 | SkTArray<JsonWriter::BitmapResult> gBitmapResults; |
| 20 | SK_DECLARE_STATIC_MUTEX(gBitmapResultLock); |
| 21 | |
| 22 | void JsonWriter::AddBitmapResult(const BitmapResult& result) { |
| 23 | SkAutoMutexAcquire lock(&gBitmapResultLock); |
| 24 | gBitmapResults.push_back(result); |
| 25 | } |
| 26 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 27 | SkTArray<skiatest::Failure> gFailures; |
| 28 | SK_DECLARE_STATIC_MUTEX(gFailureLock); |
| 29 | |
| 30 | void JsonWriter::AddTestFailure(const skiatest::Failure& failure) { |
| 31 | SkAutoMutexAcquire lock(gFailureLock); |
| 32 | gFailures.push_back(failure); |
| 33 | } |
| 34 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 35 | void JsonWriter::DumpJson() { |
| 36 | if (FLAGS_writePath.isEmpty()) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | Json::Value root; |
| 41 | |
| 42 | for (int i = 1; i < FLAGS_properties.count(); i += 2) { |
| 43 | root[FLAGS_properties[i-1]] = FLAGS_properties[i]; |
| 44 | } |
| 45 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
| 46 | root["key"][FLAGS_key[i-1]] = FLAGS_key[i]; |
| 47 | } |
| 48 | |
| 49 | { |
| 50 | SkAutoMutexAcquire lock(&gBitmapResultLock); |
| 51 | for (int i = 0; i < gBitmapResults.count(); i++) { |
| 52 | Json::Value result; |
| 53 | result["key"]["name"] = gBitmapResults[i].name.c_str(); |
| 54 | result["key"]["config"] = gBitmapResults[i].config.c_str(); |
| 55 | result["key"]["mode"] = gBitmapResults[i].mode.c_str(); |
| 56 | result["options"]["source_type"] = gBitmapResults[i].sourceType.c_str(); |
| 57 | result["md5"] = gBitmapResults[i].md5.c_str(); |
| 58 | |
| 59 | root["results"].append(result); |
| 60 | } |
| 61 | } |
| 62 | |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 63 | { |
| 64 | SkAutoMutexAcquire lock(gFailureLock); |
| 65 | for (int i = 0; i < gFailures.count(); i++) { |
| 66 | Json::Value result; |
| 67 | result["file_name"] = gFailures[i].fileName; |
| 68 | result["line_no"] = gFailures[i].lineNo; |
| 69 | result["condition"] = gFailures[i].condition; |
| 70 | result["message"] = gFailures[i].message.c_str(); |
| 71 | |
| 72 | root["test_results"]["failures"].append(result); |
| 73 | } |
| 74 | } |
| 75 | |
scroggo | 7a10fb6 | 2014-11-04 07:21:10 -0800 | [diff] [blame] | 76 | SkString path = SkOSPath::Join(FLAGS_writePath[0], "dm.json"); |
| 77 | SkFILEWStream stream(path.c_str()); |
| 78 | stream.writeText(Json::StyledWriter().write(root).c_str()); |
| 79 | stream.flush(); |
| 80 | } |
| 81 | |
| 82 | } // namespace DM |