Browse Source

Made Log thread safe
Added C# Debug.Log

Marko Pintera 11 years ago
parent
commit
fe324f98ed

+ 20 - 0
BansheeMono/Include/BsMonoUtil.h

@@ -25,11 +25,31 @@ namespace BansheeEngine
 			return ret;
 			return ret;
 		}
 		}
 
 
+		static String monoToString(MonoString* str)
+		{
+			if(str == nullptr)
+				return StringUtil::BLANK;
+
+			int len = mono_string_length(str);
+			mono_unichar2* monoChars = mono_string_chars(str);
+
+			String ret(len, '0');
+			for(int i = 0; i < len; i++)
+				ret[i] = (char)monoChars[i];
+
+			return ret;
+		}
+
 		static MonoString* wstringToMono(MonoDomain* domain, const WString& str)
 		static MonoString* wstringToMono(MonoDomain* domain, const WString& str)
 		{
 		{
 			return mono_string_from_utf16((mono_unichar2*)str.c_str());
 			return mono_string_from_utf16((mono_unichar2*)str.c_str());
 		}
 		}
 
 
+		static MonoString* stringToMono(MonoDomain* domain, const String& str)
+		{
+			return wstringToMono(domain, toWString(str));
+		}
+
 		static void throwIfException(MonoException* exception)
 		static void throwIfException(MonoException* exception)
 		{
 		{
 			throwIfException(reinterpret_cast<MonoObject*>(exception));
 			throwIfException(reinterpret_cast<MonoObject*>(exception));

+ 0 - 3
CamelotD3D9Renderer/Include/CmD3D9Prerequisites.h

@@ -30,9 +30,6 @@ THE SOFTWARE.
 
 
 #include "CmPrerequisites.h"
 #include "CmPrerequisites.h"
 
 
-#define CM_LOCK_RECURSIVE_MUTEX(name) 
-#define CM_UNLOCK_RECURSIVE_MUTEX(name)
-
 #define D3D9_DEVICE_ACCESS_LOCK	
 #define D3D9_DEVICE_ACCESS_LOCK	
 #define D3D9_DEVICE_ACCESS_UNLOCK
 #define D3D9_DEVICE_ACCESS_UNLOCK
 #define D3D9_DEVICE_ACCESS_CRITICAL_SECTION
 #define D3D9_DEVICE_ACCESS_CRITICAL_SECTION

+ 2 - 4
CamelotD3D9Renderer/Source/CmD3D9ResourceManager.cpp

@@ -140,8 +140,7 @@ namespace BansheeEngine
 		assert(mDeviceAccessLockCount >= 0);
 		assert(mDeviceAccessLockCount >= 0);
 		mDeviceAccessLockCount++;
 		mDeviceAccessLockCount++;
 		if (mDeviceAccessLockCount == 1)
 		if (mDeviceAccessLockCount == 1)
-		{					
-			CM_LOCK_RECURSIVE_MUTEX(mResourcesMutex);		
+		{						
 			D3D9Resource::lockDeviceAccess();
 			D3D9Resource::lockDeviceAccess();
 			D3D9PixelBuffer::lockDeviceAccess();
 			D3D9PixelBuffer::lockDeviceAccess();
 		}
 		}
@@ -155,8 +154,7 @@ namespace BansheeEngine
 		if (mDeviceAccessLockCount == 0)
 		if (mDeviceAccessLockCount == 0)
 		{						
 		{						
 			D3D9PixelBuffer::unlockDeviceAccess();
 			D3D9PixelBuffer::unlockDeviceAccess();
-			D3D9Resource::unlockDeviceAccess();			
-			CM_UNLOCK_RECURSIVE_MUTEX(mResourcesMutex);			
+			D3D9Resource::unlockDeviceAccess();				
 		}
 		}
 	}
 	}
 }
 }

+ 5 - 0
CamelotUtility/Include/CmLog.h

@@ -25,6 +25,8 @@ namespace BansheeEngine
 	/**
 	/**
 	 * @brief	Used for logging messages. Can categorize messages according to channels, save the log to a file
 	 * @brief	Used for logging messages. Can categorize messages according to channels, save the log to a file
 	 * 			and send out callbacks when a new message is added.
 	 * 			and send out callbacks when a new message is added.
+	 * 			
+	 * @note	Thread safe.
 	 */
 	 */
 	class CM_UTILITY_EXPORT Log
 	class CM_UTILITY_EXPORT Log
 	{
 	{
@@ -52,6 +54,7 @@ namespace BansheeEngine
 
 
 	private:
 	private:
 		Vector<LogEntry*>::type mEntries;
 		Vector<LogEntry*>::type mEntries;
+		CM_RECURSIVE_MUTEX(mMutex);
 
 
 		/**
 		/**
 		 * @brief	Called whenever a new entry is added.
 		 * @brief	Called whenever a new entry is added.
@@ -64,6 +67,8 @@ namespace BansheeEngine
 	public:
 	public:
 		/**
 		/**
 		 * @brief	Triggered when a new entry in the log is added.
 		 * @brief	Triggered when a new entry in the log is added.
+		 * 			
+		 * @note	
 		 */
 		 */
 		boost::signal<void(const LogEntry&)> onEntryAdded;
 		boost::signal<void(const LogEntry&)> onEntryAdded;
 	};
 	};

+ 3 - 0
CamelotUtility/Include/CmThreadDefines.h

@@ -31,6 +31,9 @@
 #define CM_THREAD_NOTIFY_ONE(sync) sync.notify_one(); 
 #define CM_THREAD_NOTIFY_ONE(sync) sync.notify_one(); 
 #define CM_THREAD_NOTIFY_ALL(sync) sync.notify_all(); 
 #define CM_THREAD_NOTIFY_ALL(sync) sync.notify_all(); 
 #define CM_THREAD_JOIN(thread) thread.join();
 #define CM_THREAD_JOIN(thread) thread.join();
+// Recursive mutex
+#define CM_RECURSIVE_MUTEX(name) mutable std::recursive_mutex name
+#define CM_LOCK_RECURSIVE_MUTEX(name) std::unique_lock<std::recursive_mutex> cmnameLock(name);
 // Read-write mutex
 // Read-write mutex
 #define CM_RW_MUTEX(name) mutable std::mutex name
 #define CM_RW_MUTEX(name) mutable std::mutex name
 #define CM_LOCK_RW_MUTEX_READ(name) std::unique_lock<std::mutex> cmnameLock(name)
 #define CM_LOCK_RW_MUTEX_READ(name) std::unique_lock<std::mutex> cmnameLock(name)

+ 6 - 0
CamelotUtility/Source/CmLog.cpp

@@ -14,12 +14,16 @@ namespace BansheeEngine
 
 
 	Log::~Log()
 	Log::~Log()
 	{
 	{
+		CM_LOCK_RECURSIVE_MUTEX(mMutex);
+
 		for(auto iter = mEntries.begin(); iter != mEntries.end(); ++iter)
 		for(auto iter = mEntries.begin(); iter != mEntries.end(); ++iter)
 			cm_delete<PoolAlloc>(*iter);
 			cm_delete<PoolAlloc>(*iter);
 	}
 	}
 
 
 	void Log::logMsg(const String& message, const String& level)
 	void Log::logMsg(const String& message, const String& level)
 	{
 	{
+		CM_LOCK_RECURSIVE_MUTEX(mMutex);
+
 		LogEntry* newEntry = cm_new<LogEntry, PoolAlloc>(message, level);
 		LogEntry* newEntry = cm_new<LogEntry, PoolAlloc>(message, level);
 		mEntries.push_back(newEntry);
 		mEntries.push_back(newEntry);
 
 
@@ -28,6 +32,8 @@ namespace BansheeEngine
 
 
 	void Log::clear()
 	void Log::clear()
 	{
 	{
+		CM_LOCK_RECURSIVE_MUTEX(mMutex);
+
 		for(auto iter = mEntries.begin(); iter != mEntries.end(); ++iter)
 		for(auto iter = mEntries.begin(); iter != mEntries.end(); ++iter)
 			cm_delete<PoolAlloc>(*iter);
 			cm_delete<PoolAlloc>(*iter);
 
 

+ 18 - 3
MBansheeEngine/Debug.cs

@@ -1,20 +1,35 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using System.Text;
 using System.Text;
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
-    public class Debug
+    public sealed class Debug
     {
     {
         public static void Log(string message)
         public static void Log(string message)
         {
         {
-            // TODO - Not implemented
+            Internal_Log(message);
         }
         }
 
 
         public static void LogWarning(string message)
         public static void LogWarning(string message)
         {
         {
-            // TODO - Not implemented
+            Internal_LogWarning(message);
         }
         }
+
+        public static void LogError(string message)
+        {
+            Internal_LogError(message);
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        internal static extern Component Internal_Log(string message);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        internal static extern Component Internal_LogWarning(string message);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        internal static extern Component Internal_LogError(string message);
     }
     }
 }
 }

+ 2 - 0
MBansheeEngine/Program.cs

@@ -107,6 +107,8 @@ namespace BansheeEngine
             //dbgStyle.textColor = newColor;
             //dbgStyle.textColor = newColor;
             //Color myColor = dbgStyle.textColor;
             //Color myColor = dbgStyle.textColor;
             //dbgStyle.textColor = myColor;
             //dbgStyle.textColor = myColor;
+
+            //SerializableObject obj = new SerializableObject(new DbgSerzCls());
         }
         }
 
 
         [MethodImpl(MethodImplOptions.InternalCall)]
         [MethodImpl(MethodImplOptions.InternalCall)]

+ 20 - 0
SBansheeEngine/Include/BsScriptDebug.h

@@ -0,0 +1,20 @@
+#pragma once
+
+#include "BsScriptEnginePrerequisites.h"
+#include "BsScriptObject.h"
+
+namespace BansheeEngine
+{
+	class BS_SCR_BE_EXPORT ScriptDebug : public ScriptObject<ScriptDebug>
+	{
+	public:
+		static void initMetaData();
+
+	private:
+		static void internal_log(MonoString* message);
+		static void internal_logWarning(MonoString* message);
+		static void internal_logError(MonoString* message);
+
+		static void initRuntimeData();
+	};
+}

+ 2 - 0
SBansheeEngine/SBansheeEngine.vcxproj

@@ -239,6 +239,7 @@
     <ClInclude Include="Include\BsManagedSerializableObject.h" />
     <ClInclude Include="Include\BsManagedSerializableObject.h" />
     <ClInclude Include="Include\BsRuntimeScriptObjects.h" />
     <ClInclude Include="Include\BsRuntimeScriptObjects.h" />
     <ClInclude Include="Include\BsScriptComponent.h" />
     <ClInclude Include="Include\BsScriptComponent.h" />
+    <ClInclude Include="Include\BsScriptDebug.h" />
     <ClInclude Include="Include\BsScriptEnginePrerequisites.h" />
     <ClInclude Include="Include\BsScriptEnginePrerequisites.h" />
     <ClInclude Include="Include\BsScriptGameObject.h" />
     <ClInclude Include="Include\BsScriptGameObject.h" />
     <ClInclude Include="Include\BsScriptGameObjectManager.h" />
     <ClInclude Include="Include\BsScriptGameObjectManager.h" />
@@ -278,6 +279,7 @@
     <ClCompile Include="Source\BsManagedComponent.cpp" />
     <ClCompile Include="Source\BsManagedComponent.cpp" />
     <ClCompile Include="Source\BsRuntimeScriptObjects.cpp" />
     <ClCompile Include="Source\BsRuntimeScriptObjects.cpp" />
     <ClCompile Include="Source\BsScriptComponent.cpp" />
     <ClCompile Include="Source\BsScriptComponent.cpp" />
+    <ClCompile Include="Source\BsScriptDebug.cpp" />
     <ClCompile Include="Source\BsScriptEnginePlugin.cpp" />
     <ClCompile Include="Source\BsScriptEnginePlugin.cpp" />
     <ClCompile Include="Source\BsScriptGameObjectManager.cpp" />
     <ClCompile Include="Source\BsScriptGameObjectManager.cpp" />
     <ClCompile Include="Source\BsScriptGUIButton.cpp" />
     <ClCompile Include="Source\BsScriptGUIButton.cpp" />

+ 6 - 0
SBansheeEngine/SBansheeEngine.vcxproj.filters

@@ -171,6 +171,9 @@
     <ClInclude Include="Include\BsScriptSerializableField.h">
     <ClInclude Include="Include\BsScriptSerializableField.h">
       <Filter>Header Files</Filter>
       <Filter>Header Files</Filter>
     </ClInclude>
     </ClInclude>
+    <ClInclude Include="Include\BsScriptDebug.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsScriptTexture2D.cpp">
     <ClCompile Include="Source\BsScriptTexture2D.cpp">
@@ -281,5 +284,8 @@
     <ClCompile Include="Source\BsScriptSerializableField.cpp">
     <ClCompile Include="Source\BsScriptSerializableField.cpp">
       <Filter>Source Files</Filter>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
+    <ClCompile Include="Source\BsScriptDebug.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 37 - 0
SBansheeEngine/Source/BsScriptDebug.cpp

@@ -0,0 +1,37 @@
+#include "BsScriptDebug.h"
+#include "BsMonoManager.h"
+#include "BsMonoClass.h"
+#include "BsMonoUtil.h"
+#include "CmDebug.h"
+
+namespace BansheeEngine
+{
+	void ScriptDebug::initMetaData()
+	{
+		metaData = ScriptMeta(BansheeEngineAssemblyName, "BansheeEngine", "Debug", &ScriptDebug::initRuntimeData);
+
+		MonoManager::registerScriptType(&metaData);
+	}
+
+	void ScriptDebug::initRuntimeData()
+	{
+		metaData.scriptClass->addInternalCall("Internal_Log", &ScriptDebug::internal_log);
+		metaData.scriptClass->addInternalCall("Internal_LogWarning", &ScriptDebug::internal_logWarning);
+		metaData.scriptClass->addInternalCall("Internal_LogError", &ScriptDebug::internal_logError);
+	}
+
+	void ScriptDebug::internal_log(MonoString* message)
+	{
+		gDebug().log(MonoUtil::monoToString(message), "ScriptInfo");
+	}
+
+	void ScriptDebug::internal_logWarning(MonoString* message)
+	{
+		gDebug().log(MonoUtil::monoToString(message), "ScriptWarning");
+	}
+
+	void ScriptDebug::internal_logError(MonoString* message)
+	{
+		gDebug().log(MonoUtil::monoToString(message), "ScriptError");
+	}
+}