| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // \copyright | ||
| 2 | // Copyright 2009-2015, by the California Institute of Technology. | ||
| 3 | // ALL RIGHTS RESERVED. United States Government Sponsorship | ||
| 4 | // acknowledged. | ||
| 5 | |||
| 6 | #include <Fw/Types/Assert.hpp> | ||
| 7 | #include <Fw/Types/StringUtils.hpp> | ||
| 8 | #include <Os/File.hpp> | ||
| 9 | #include <Os/FileSystem.hpp> | ||
| 10 | #include <Svc/ActiveTextLogger/LogFile.hpp> | ||
| 11 | #include <cstdio> | ||
| 12 | #include <cstring> | ||
| 13 | #include <limits> | ||
| 14 | |||
| 15 | namespace Svc { | ||
| 16 | |||
| 17 | // ---------------------------------------------------------------------- | ||
| 18 | // Initialization/Exiting | ||
| 19 | // ---------------------------------------------------------------------- | ||
| 20 | |||
| 21 |
1/1✓ Branch 5 taken 5 times.
|
5 | LogFile::LogFile() : m_fileName(), m_file(), m_maxFileSize(0), m_openFile(false), m_currentFileSize(0) {} |
| 22 | |||
| 23 | 10 | LogFile::~LogFile() { | |
| 24 | // Close the file if needed: | ||
| 25 |
2/4✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
|
5 | if (this->m_openFile) { |
| 26 | 5 | this->m_file.close(); | |
| 27 | } | ||
| 28 | 10 | } | |
| 29 | |||
| 30 | // ---------------------------------------------------------------------- | ||
| 31 | // Member Functions | ||
| 32 | // ---------------------------------------------------------------------- | ||
| 33 | |||
| 34 | 10 | bool LogFile::write_to_log(const char* const buf, const FwSizeType size) { | |
| 35 | 10 | FW_ASSERT(buf != nullptr); | |
| 36 | |||
| 37 | 10 | bool status = true; | |
| 38 | |||
| 39 | // Print to file if there is one, and given a valid size: | ||
| 40 |
4/6✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
|
10 | if (this->m_openFile && size > 0) { |
| 41 | // Make sure we won't exceed the maximum size: | ||
| 42 | // Note: second condition in if statement is true if there is overflow | ||
| 43 | // in the addition below | ||
| 44 | 7 | FwSizeType projectedSize = this->m_currentFileSize + size; | |
| 45 |
4/4✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 6 times.
|
13 | if (projectedSize > this->m_maxFileSize || |
| 46 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
|
6 | (this->m_currentFileSize > (std::numeric_limits<FwSizeType>::max() - size))) { |
| 47 | 1 | status = false; | |
| 48 | 1 | this->m_openFile = false; | |
| 49 | 1 | this->m_file.close(); | |
| 50 | } | ||
| 51 | // Won't exceed max size, so write to file: | ||
| 52 | else { | ||
| 53 | 6 | FwSizeType writeSize = size; | |
| 54 |
1/1✓ Branch 6 taken 6 times.
|
6 | Os::File::Status stat = this->m_file.write(reinterpret_cast<const U8*>(buf), writeSize, Os::File::WAIT); |
| 55 | |||
| 56 | // Assert that we are not trying to write to a file we never opened: | ||
| 57 | 6 | FW_ASSERT(stat != Os::File::NOT_OPENED); | |
| 58 | |||
| 59 | // Only return a good status if the write was valid | ||
| 60 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | status = (stat == Os::File::OP_OK) && (static_cast<FwSizeType>(writeSize) == size); |
| 61 | |||
| 62 | 6 | this->m_currentFileSize += static_cast<FwSizeType>(writeSize); | |
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | 10 | return status; | |
| 67 | } | ||
| 68 | |||
| 69 | 21 | bool LogFile::set_log_file(const char* fileName, const FwSizeType maxSize, const FwSizeType maxBackups) { | |
| 70 | 21 | FW_ASSERT(fileName != nullptr); | |
| 71 | |||
| 72 | // If there is already a previously open file then close it: | ||
| 73 |
3/4✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 8 times.
|
21 | if (this->m_openFile) { |
| 74 | 13 | this->m_openFile = false; | |
| 75 |
1/1✓ Branch 6 taken 13 times.
|
13 | this->m_file.close(); |
| 76 | } | ||
| 77 |
1/1✓ Branch 2 taken 21 times.
|
21 | Fw::FileNameString searchFilename; |
| 78 |
1/1✓ Branch 2 taken 21 times.
|
21 | Fw::FormatStatus formatStatus = searchFilename.format("%s", fileName); |
| 79 | |||
| 80 | // If file name is too large, return failure: | ||
| 81 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
|
21 | if (formatStatus != Fw::FormatStatus::SUCCESS) { |
| 82 | 1 | return false; | |
| 83 | } | ||
| 84 | |||
| 85 | // Check if file already exists, and if it does try to tack on a suffix. | ||
| 86 | // Quit after maxBackups suffix addition tries (first try is w/ the original name). | ||
| 87 | 20 | bool failedSuffix = true; | |
| 88 | 20 | FwSizeType fileSize = 0; | |
| 89 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 1 times.
|
87 | for (U32 suffix = 0; suffix <= maxBackups; ++suffix) { |
| 90 | // A non-existing file was found, so use it: | ||
| 91 |
3/3✓ Branch 3 taken 86 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 68 times.
|
86 | if (Os::FileSystem::getFileSize(searchFilename.toChar(), fileSize) != Os::FileSystem::OP_OK) { |
| 92 | 18 | failedSuffix = false; | |
| 93 | 18 | break; | |
| 94 | } | ||
| 95 | // Format and check for error and overflows | ||
| 96 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 1 times.
|
68 | if (suffix < maxBackups) { |
| 97 |
1/1✓ Branch 2 taken 67 times.
|
67 | formatStatus = searchFilename.format("%s%" PRIu32, fileName, suffix); |
| 98 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 66 times.
|
67 | if (formatStatus != Fw::FormatStatus::SUCCESS) { |
| 99 | 1 | return false; | |
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | // If failed trying to make a new file, just use the original file | ||
| 105 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18 times.
|
19 | if (failedSuffix) { |
| 106 |
1/1✓ Branch 2 taken 1 times.
|
1 | searchFilename = fileName; |
| 107 | } | ||
| 108 | |||
| 109 | // Open the file (using CREATE so that it truncates an already existing file): | ||
| 110 | Os::File::Status stat = | ||
| 111 |
1/1✓ Branch 8 taken 19 times.
|
19 | this->m_file.open(searchFilename.toChar(), Os::File::OPEN_CREATE, Os::File::OverwriteType::OVERWRITE); |
| 112 | |||
| 113 | // Bad status when trying to open the file: | ||
| 114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
|
19 | if (stat != Os::File::OP_OK) { |
| 115 | ✗ | return false; | |
| 116 | } | ||
| 117 | |||
| 118 | 19 | this->m_currentFileSize = 0; | |
| 119 | 19 | this->m_maxFileSize = maxSize; | |
| 120 |
1/1✓ Branch 5 taken 19 times.
|
19 | this->m_fileName = searchFilename; |
| 121 | 19 | this->m_openFile = true; | |
| 122 | |||
| 123 | 19 | return true; | |
| 124 | 21 | } | |
| 125 | |||
| 126 | } // namespace Svc | ||
| 127 |