changeset 2087:e9e6ffbf0fd5

improved logging in FilesystemStorage
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Sep 2016 11:53:17 +0200
parents 630606097798
children b9428d5f7eaf
files Core/FileStorage/FilesystemStorage.cpp Core/Toolbox.cpp
diffstat 2 files changed, 40 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/Core/FileStorage/FilesystemStorage.cpp	Fri Sep 16 09:18:35 2016 +0200
+++ b/Core/FileStorage/FilesystemStorage.cpp	Fri Sep 16 11:53:17 2016 +0200
@@ -86,11 +86,37 @@
     Toolbox::MakeDirectory(root);
   }
 
+
+
+  static const char* GetDescriptionInternal(FileContentType content)
+  {
+    // This function is for logging only (internal use), a more
+    // fully-featured version is available in ServerEnumerations.cpp
+    switch (content)
+    {
+      case FileContentType_Unknown:
+        return "Unknown";
+
+      case FileContentType_Dicom:
+        return "DICOM";
+
+      case FileContentType_DicomAsJson:
+        return "JSON summary of DICOM";
+
+      default:
+        return "User-defined";
+    }
+  }
+
+
   void FilesystemStorage::Create(const std::string& uuid,
                                  const void* content, 
                                  size_t size,
-                                 FileContentType /*type*/)
+                                 FileContentType type)
   {
+    LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type) 
+              << "\" type (size: " << (size / (1024 * 1024) + 1) << "MB)";
+
     boost::filesystem::path path;
     
     path = GetPath(uuid);
@@ -117,31 +143,17 @@
       }
     }
 
-    boost::filesystem::ofstream f;
-    f.open(path, std::ofstream::out | std::ios::binary);
-    if (!f.good())
-    {
-      throw OrthancException(ErrorCode_FileStorageCannotWrite);
-    }
-
-    if (size != 0)
-    {
-      f.write(static_cast<const char*>(content), size);
-      if (!f.good())
-      {
-        f.close();
-        throw OrthancException(ErrorCode_FileStorageCannotWrite);
-      }
-    }
-
-    f.close();
+    Toolbox::WriteFile(content, size, path.string());
   }
 
 
   void FilesystemStorage::Read(std::string& content,
                                const std::string& uuid,
-                               FileContentType /*type*/)
+                               FileContentType type)
   {
+    LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type) 
+              << "\" content type";
+
     content.clear();
     Toolbox::ReadFile(content, GetPath(uuid).string());
   }
@@ -213,9 +225,7 @@
   void FilesystemStorage::Remove(const std::string& uuid,
                                  FileContentType /*type*/)
   {
-#if ORTHANC_ENABLE_GOOGLE_LOG == 1
-    LOG(INFO) << "Deleting file " << uuid;
-#endif
+    LOG(INFO) << "Deleting attachment \"" << uuid << "\"";
 
     namespace fs = boost::filesystem;
 
--- a/Core/Toolbox.cpp	Fri Sep 16 09:18:35 2016 +0200
+++ b/Core/Toolbox.cpp	Fri Sep 16 11:53:17 2016 +0200
@@ -312,7 +312,7 @@
                           const std::string& path)
   {
     boost::filesystem::ofstream f;
-    f.open(path, std::ofstream::binary);
+    f.open(path, std::ofstream::out | std::ofstream::binary);
     if (!f.good())
     {
       throw OrthancException(ErrorCode_CannotWriteFile);
@@ -321,6 +321,12 @@
     if (size != 0)
     {
       f.write(reinterpret_cast<const char*>(content), size);
+
+      if (!f.good())
+      {
+        f.close();
+        throw OrthancException(ErrorCode_FileStorageCannotWrite);
+      }
     }
 
     f.close();