]> git.sesse.net Git - casparcg/commitdiff
* Broke out platform specific parts of main.cpp to platform_specific.h with implement...
authorHelge Norberg <helge.norberg@svt.se>
Fri, 10 Apr 2015 13:30:31 +0000 (15:30 +0200)
committerHelge Norberg <helge.norberg@svt.se>
Fri, 10 Apr 2015 13:30:31 +0000 (15:30 +0200)
14 files changed:
accelerator/StdAfx.h
core/StdAfx.h
modules/bluefish/StdAfx.h
modules/decklink/StdAfx.h
modules/ffmpeg/StdAfx.h
modules/flash/StdAfx.h
protocol/StdAfx.h
protocol/amcp/AMCPProtocolStrategy.cpp
shell/CMakeLists.txt
shell/linux_specific.cpp [new file with mode: 0644]
shell/main.cpp
shell/platform_specific.h [new file with mode: 0644]
shell/server.cpp
shell/windows_specific.cpp [new file with mode: 0644]

index 4858c3489729f8136d624ea07bc26c61b6769e3b..e44e81e2602a5942c30231214bb0cf235a966ac7 100644 (file)
@@ -21,7 +21,7 @@
 
 #pragma once
 
-#ifdef _DEBUG
+#if defined _DEBUG && defined _MSC_VER
 #include <crtdbg.h>
 #endif
 
index c7a2f310fc280487aab122a2032b012d7ad5d8d5..6bbcf67b6890887f5984109d29e212148f451cdf 100644 (file)
@@ -21,7 +21,7 @@
 
 #pragma once
 
-#ifdef _DEBUG
+#if defined _DEBUG && defined _MSC_VER
 #include <crtdbg.h>
 #endif
 
index 90c47c4d4b6b72c0f1f32150ea8c9b7d2146b567..abe7a43305c047830927819491285f143b922173 100644 (file)
@@ -21,7 +21,7 @@
 
 #pragma once
 
-#ifdef _DEBUG
+#if defined _DEBUG && defined _MSC_VER
 #include <crtdbg.h>
 #endif
 
index c8712ba5affd7c1bbc4fe9bf4d8063f9fd38bee6..1d335feed8ee6d19313db5fa67fd1aa6c458a103 100644 (file)
@@ -22,7 +22,7 @@
 #pragma once
 
 #ifdef __cplusplus
-#ifdef _DEBUG
+#if defined _DEBUG && defined _MSC_VER
 #include <crtdbg.h>
 #endif
 
index 81976b5404a3f23684d55cb3604c253d8001437e..88ae4e8ad56a14aeefb91bcf54538dcf5a38791a 100644 (file)
@@ -21,7 +21,7 @@
 
 #pragma once
 
-#ifdef _DEBUG
+#if defined _DEBUG && defined _MSC_VER
 #include <crtdbg.h>
 #endif
 
index 0f8a3ec88cbdb7b6ffdea8780d0368e58b5cfbd0..e1cbb550f4857887d2e167fe65b9f4867fec199b 100644 (file)
@@ -27,7 +27,7 @@
 
 #include "../common/compiler/vs/disable_silly_warnings.h"
 
-#ifdef _DEBUG
+#if defined _DEBUG && defined _MSC_VER
 #include <crtdbg.h>
 #endif
 
index df1582008b43e6ccb8789c80f084becdf57595e0..8b09ded289d952097cbb2a2692edc43a8ed7a510 100644 (file)
@@ -33,7 +33,7 @@
 #      endif
 #endif
 
-#ifdef _DEBUG
+#if defined _DEBUG && defined _MSC_VER
 #include <crtdbg.h>
 #endif
 
index 140e266e5bcbca03e99a964e06ad6ee7768bbede..f60433bd236eaaa2f7edb71e3049c312f4c90615 100644 (file)
@@ -29,7 +29,6 @@
 #include "AMCPCommandQueue.h"
 
 #include <stdio.h>
-//#include <crtdbg.h>
 #include <string.h>
 #include <algorithm>
 #include <cctype>
index 4919abd2e42fb58294f936f377a73aaa4f399a68..1cec0693dab04d63204884f836225085cda1d346 100644 (file)
@@ -10,9 +10,11 @@ if (MSVC)
        set(OS_SPECIFIC_SOURCES
                        resource.h
                        shell.rc
+                       windows_specific.cpp
        )
 elseif (CMAKE_COMPILER_IS_GNUCXX)
        set(OS_SPECIFIC_SOURCES
+                       linux_specific.cpp
        )
 endif ()
 set(SOURCES
@@ -22,6 +24,7 @@ set(SOURCES
 )
 set(HEADERS
                included_modules.h
+               platform_specific.h
                server.h
                stdafx.h
 )
diff --git a/shell/linux_specific.cpp b/shell/linux_specific.cpp
new file mode 100644 (file)
index 0000000..c50123d
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Helge Norberg, helge.norberg@svt.se
+*/
+
+#include "stdafx.h"
+
+#include "platform_specific.h"
+
+namespace caspar {
+
+void setup_prerequisites()
+{
+
+}
+
+void setup_console_window()
+{
+
+}
+
+void increase_process_priority()
+{
+}
+
+void wait_for_keypress()
+{
+}
+
+std::shared_ptr<void> setup_debugging_environment()
+{
+       return nullptr;
+}
+
+void wait_for_remote_debugging()
+{
+
+}
+
+}
index 2e0875a1cf5a333e8fabf2a428f5455e7b0d91ec..88950e6ec617a072f8b071475d65ce738d745a37 100644 (file)
@@ -28,7 +28,7 @@
 #include <tbb/task_scheduler_init.h>
 #include <tbb/task_scheduler_observer.h>
 
-#ifdef _DEBUG
+#if defined _DEBUG && defined _MSC_VER
        #define _CRTDBG_MAP_ALLOC
        #include <stdlib.h>
        #include <crtdbg.h>
        #include <tbb/tbbmalloc_proxy.h>
 #endif
 
-#include "resource.h"
-
 #include "server.h"
-
-#include <common/os/windows/windows.h>
-#include <winnt.h>
-#include <mmsystem.h>
-#include <atlbase.h>
+#include "platform_specific.h"
 
 #include <protocol/util/strategy_adapters.h>
 #include <protocol/amcp/AMCPProtocolStrategy.h>
 
 #include <future>
 
-#include <signal.h>
+#include <csignal>
 
 using namespace caspar;
        
-// NOTE: This is needed in order to make CComObject work since this is not a real ATL project.
-CComModule _AtlModule;
-extern __declspec(selectany) CAtlModule* _pAtlModule = &_AtlModule;
-
-void change_icon( const HICON hNewIcon )
-{
-   auto hMod = ::LoadLibrary(L"Kernel32.dll"); 
-   typedef DWORD(__stdcall *SCI)(HICON);
-   auto pfnSetConsoleIcon = reinterpret_cast<SCI>(::GetProcAddress(hMod, "SetConsoleIcon")); 
-   pfnSetConsoleIcon(hNewIcon); 
-   ::FreeLibrary(hMod);
-}
-
 void setup_global_locale()
 {
        boost::locale::generator gen;
@@ -93,43 +74,6 @@ void setup_global_locale()
        std::locale::global(gen(""));
 }
 
-void setup_console_window()
-{       
-       auto hOut = GetStdHandle(STD_OUTPUT_HANDLE);
-
-       // Disable close button in console to avoid shutdown without cleanup.
-       EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
-       DrawMenuBar(GetConsoleWindow());
-       //SetConsoleCtrlHandler(HandlerRoutine, true);
-
-       // Configure console size and position.
-       auto coord = GetLargestConsoleWindowSize(hOut);
-       coord.X /= 2;
-
-       SetConsoleScreenBufferSize(hOut, coord);
-
-       SMALL_RECT DisplayArea = {0, 0, 0, 0};
-       DisplayArea.Right = coord.X-1;
-       DisplayArea.Bottom = (coord.Y-1)/2;
-       SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
-                
-       change_icon(::LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(101)));
-
-       // Set console title.
-       std::wstringstream str;
-       str << "CasparCG Server " << env::version() << L" x64 ";
-#ifdef COMPILE_RELEASE
-       str << " Release";
-#elif  COMPILE_PROFILE
-       str << " Profile";
-#elif  COMPILE_DEVELOP
-       str << " Develop";
-#elif  COMPILE_DEBUG
-       str << " Debug";
-#endif
-       SetConsoleTitle(str.str().c_str());
-}
-
 void print_info()
 {
        CASPAR_LOG(info) << L"############################################################################";
@@ -166,24 +110,6 @@ void print_system_info(const spl::shared_ptr<core::system_info_provider_reposito
                print_child(L"", elem.first, elem.second);
 }
 
-LONG WINAPI UserUnhandledExceptionFilter(EXCEPTION_POINTERS* info)
-{
-       try
-       {
-               CASPAR_LOG(fatal) << L"#######################\n UNHANDLED EXCEPTION: \n" 
-                       << L"Adress:" << info->ExceptionRecord->ExceptionAddress << L"\n"
-                       << L"Code:" << info->ExceptionRecord->ExceptionCode << L"\n"
-                       << L"Flag:" << info->ExceptionRecord->ExceptionFlags << L"\n"
-                       << L"Info:" << info->ExceptionRecord->ExceptionInformation << L"\n"
-                       << L"Continuing execution. \n#######################";
-
-               CASPAR_LOG_CALL_STACK();
-       }
-       catch(...){}
-
-       return EXCEPTION_CONTINUE_EXECUTION;
-}
-
 void do_run(server& caspar_server, std::promise<bool>& shutdown_server_now)
 {
        // Create a dummy client which prints amcp responses to console.
@@ -337,42 +263,23 @@ void on_abort(int)
 int main(int argc, wchar_t* argv[])
 {      
        int return_code = 0;
-       SetUnhandledExceptionFilter(UserUnhandledExceptionFilter);
-       signal(SIGABRT, on_abort);
+       setup_prerequisites();
+       std::signal(SIGABRT, on_abort);
 
        setup_global_locale();
 
        std::wcout << L"Type \"q\" to close application." << std::endl;
        
        // Set debug mode.
-       #ifdef _DEBUG
-               HANDLE hLogFile;
-               hLogFile = CreateFile(L"crt_log.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-               std::shared_ptr<void> crt_log(nullptr, [](HANDLE h){::CloseHandle(h);});
-
-               _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
-               _CrtSetReportMode(_CRT_WARN,    _CRTDBG_MODE_FILE);
-               _CrtSetReportFile(_CRT_WARN,    hLogFile);
-               _CrtSetReportMode(_CRT_ERROR,   _CRTDBG_MODE_FILE);
-               _CrtSetReportFile(_CRT_ERROR,   hLogFile);
-               _CrtSetReportMode(_CRT_ASSERT,  _CRTDBG_MODE_FILE);
-               _CrtSetReportFile(_CRT_ASSERT,  hLogFile);
-       #endif
+       auto debugging_environment = setup_debugging_environment();
 
        // Increase process priotity.
-       SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
+       increase_process_priority();
 
-       // Install structured exception handler.
+       // Install general protection fault handler.
        ensure_gpf_handler_installed_for_thread("main thread");
                                
-       // Increase time precision. This will increase accuracy of function like Sleep(1) from 10 ms to 1 ms.
-       struct inc_prec
-       {
-               inc_prec(){timeBeginPeriod(1);}
-               ~inc_prec(){timeEndPeriod(1);}
-       } inc_prec;     
-
-       // Install SEH into all tbb threads.
+       // Install GPF handler into all tbb threads.
        struct tbb_thread_installer : public tbb::task_scheduler_observer
        {
                tbb_thread_installer(){observe(true);}
@@ -391,10 +298,8 @@ int main(int argc, wchar_t* argv[])
                                
                log::set_log_level(env::properties().get(L"configuration.log-level", L"debug"));
 
-       #ifdef _DEBUG
-               if(env::properties().get(L"configuration.debugging.remote", false))
-                       MessageBox(nullptr, L"Now is the time to connect for remote debugging...", L"Debug", MB_OK | MB_TOPMOST);
-       #endif   
+               if (env::properties().get(L"configuration.debugging.remote", false))
+                       wait_for_remote_debugging();
 
                // Start logging to file.
                log::add_file_sink(env::log_folder());                  
@@ -405,23 +310,23 @@ int main(int argc, wchar_t* argv[])
 
                return_code = run() ? 5 : 0;
                
-               Sleep(500);
+               boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
                CASPAR_LOG(info) << "Successfully shutdown CasparCG Server.";
        }
        catch(boost::property_tree::file_parser_error&)
        {
                CASPAR_LOG_CURRENT_EXCEPTION();
                CASPAR_LOG(fatal) << L"Unhandled configuration error in main thread. Please check the configuration file (casparcg.config) for errors.";
-               system("pause");        
+               wait_for_keypress();
        }
        catch(...)
        {
                CASPAR_LOG_CURRENT_EXCEPTION();
                CASPAR_LOG(fatal) << L"Unhandled exception in main thread. Please report this error on the CasparCG forums (www.casparcg.com/forum).";
-               Sleep(1000);
+               boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
                std::wcout << L"\n\nCasparCG will automatically shutdown. See the log file located at the configured log-file folder for more information.\n\n";
-               Sleep(4000);
-       }               
+               boost::this_thread::sleep_for(boost::chrono::milliseconds(4000));
+       }
        
        return return_code;
 }
diff --git a/shell/platform_specific.h b/shell/platform_specific.h
new file mode 100644 (file)
index 0000000..931cce7
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Helge Norberg, helge.norberg@svt.se
+*/
+
+#pragma once
+
+#include <memory>
+
+namespace caspar {
+
+void setup_prerequisites();
+void setup_console_window();
+void increase_process_priority();
+void wait_for_keypress();
+std::shared_ptr<void> setup_debugging_environment();
+void wait_for_remote_debugging();
+
+}
index aadb90dc49daee52b83800e58dfd8c8d93622a0a..394adda0bf24167da043d6a9a8d664dd9e1c1207 100644 (file)
@@ -18,6 +18,7 @@
 *
 * Author: Robert Nagy, ronag89@gmail.com
 */
+
 #include "stdafx.h"
 
 #include "server.h"
diff --git a/shell/windows_specific.cpp b/shell/windows_specific.cpp
new file mode 100644 (file)
index 0000000..668d210
--- /dev/null
@@ -0,0 +1,158 @@
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "stdafx.h"
+
+#include "platform_specific.h"
+#include "resource.h"
+
+#include <common/os/windows/windows.h>
+#include <common/env.h>
+#include <common/log.h>
+
+#include <winnt.h>
+#include <mmsystem.h>
+#include <atlbase.h>
+
+#include <sstream>
+#include <cstdlib>
+
+// NOTE: This is needed in order to make CComObject work since this is not a real ATL project.
+CComModule _AtlModule;
+extern __declspec(selectany) CAtlModule* _pAtlModule = &_AtlModule;
+
+namespace caspar {
+
+LONG WINAPI UserUnhandledExceptionFilter(EXCEPTION_POINTERS* info)
+{
+       try
+       {
+               CASPAR_LOG(fatal) << L"#######################\n UNHANDLED EXCEPTION: \n"
+                       << L"Adress:" << info->ExceptionRecord->ExceptionAddress << L"\n"
+                       << L"Code:" << info->ExceptionRecord->ExceptionCode << L"\n"
+                       << L"Flag:" << info->ExceptionRecord->ExceptionFlags << L"\n"
+                       << L"Info:" << info->ExceptionRecord->ExceptionInformation << L"\n"
+                       << L"Continuing execution. \n#######################";
+
+               CASPAR_LOG_CALL_STACK();
+       }
+       catch (...){}
+
+       return EXCEPTION_CONTINUE_EXECUTION;
+}
+
+void setup_prerequisites()
+{
+       SetUnhandledExceptionFilter(UserUnhandledExceptionFilter);
+
+       // Increase time precision. This will increase accuracy of function like Sleep(1) from 10 ms to 1 ms.
+       static struct inc_prec
+       {
+               inc_prec(){ timeBeginPeriod(1); }
+               ~inc_prec(){ timeEndPeriod(1); }
+       } inc_prec;
+}
+
+void change_icon(const HICON hNewIcon)
+{
+       auto hMod = ::LoadLibrary(L"Kernel32.dll");
+       typedef DWORD(__stdcall *SCI)(HICON);
+       auto pfnSetConsoleIcon = reinterpret_cast<SCI>(::GetProcAddress(hMod, "SetConsoleIcon"));
+       pfnSetConsoleIcon(hNewIcon);
+       ::FreeLibrary(hMod);
+}
+
+void setup_console_window()
+{
+       auto hOut = GetStdHandle(STD_OUTPUT_HANDLE);
+
+       // Disable close button in console to avoid shutdown without cleanup.
+       EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_GRAYED);
+       DrawMenuBar(GetConsoleWindow());
+       //SetConsoleCtrlHandler(HandlerRoutine, true);
+
+       // Configure console size and position.
+       auto coord = GetLargestConsoleWindowSize(hOut);
+       coord.X /= 2;
+
+       SetConsoleScreenBufferSize(hOut, coord);
+
+       SMALL_RECT DisplayArea = { 0, 0, 0, 0 };
+       DisplayArea.Right = coord.X - 1;
+       DisplayArea.Bottom = (coord.Y - 1) / 2;
+       SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
+
+       change_icon(::LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(101)));
+
+       // Set console title.
+       std::wstringstream str;
+       str << "CasparCG Server " << env::version() << L" x64 ";
+#ifdef COMPILE_RELEASE
+       str << " Release";
+#elif  COMPILE_PROFILE
+       str << " Profile";
+#elif  COMPILE_DEVELOP
+       str << " Develop";
+#elif  COMPILE_DEBUG
+       str << " Debug";
+#endif
+       SetConsoleTitle(str.str().c_str());
+}
+
+void increase_process_priority()
+{
+       SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
+}
+
+void wait_for_keypress()
+{
+       std::system("pause");
+}
+
+std::shared_ptr<void> setup_debugging_environment()
+{
+#ifdef _DEBUG
+       HANDLE hLogFile;
+       hLogFile = CreateFile(L"crt_log.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+       std::shared_ptr<void> crt_log(nullptr, [](HANDLE h){::CloseHandle(h); });
+
+       _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
+       _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
+       _CrtSetReportFile(_CRT_WARN, hLogFile);
+       _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
+       _CrtSetReportFile(_CRT_ERROR, hLogFile);
+       _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
+       _CrtSetReportFile(_CRT_ASSERT, hLogFile);
+
+       return crt_log;
+#else
+       return nullptr;
+#endif
+}
+
+void wait_for_remote_debugging()
+{
+#ifdef _DEBUG
+       MessageBox(nullptr, L"Now is the time to connect for remote debugging...", L"Debug", MB_OK | MB_TOPMOST);
+#endif  
+}
+
+}