EAMainPrintManager.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // EAMainPrintManager.cpp
  3. //
  4. // Copyright (c) 2012 Electronic Arts Inc.
  5. ///////////////////////////////////////////////////////////////////////////////
  6. #include <EAMain/EAMain.h>
  7. #include <EAMain/internal/EAMainPrintManager.h>
  8. #include <EAMain/internal/EAMainChannels.h>
  9. #include <internal/NetworkChannel.h>
  10. #include <EAAssert/eaassert.h>
  11. #include <EAStdC/EAString.h>
  12. #include <EAMainPrintf.h>
  13. #include <EABase/eabase.h>
  14. EA_DISABLE_ALL_VC_WARNINGS()
  15. #include <string.h>
  16. EA_RESTORE_ALL_VC_WARNINGS()
  17. namespace EA {
  18. namespace EAMain {
  19. //------------------------------------------------------------
  20. // STATICS
  21. //------------------------------------------------------------
  22. static PrintManager gPrintManager;
  23. static PrintfChannel gPrintfChannel;
  24. static FileChannel gFileChannel;
  25. //------------------------------------------------------------
  26. //------------------------------------------------------------
  27. PrintManager& PrintManager::Instance()
  28. {
  29. return gPrintManager;
  30. }
  31. //------------------------------------------------------------
  32. PrintManager::PrintManager()
  33. {
  34. memset(m_Channels, 0, sizeof(m_Channels));
  35. }
  36. //------------------------------------------------------------
  37. void PrintManager::Send(const char8_t* pData)
  38. {
  39. // Broadcast the message to all the registered channels.
  40. for(int i = 0; i < CHANNEL_MAX; i++)
  41. {
  42. if(m_Channels[i])
  43. m_Channels[i]->Send(pData);
  44. }
  45. }
  46. //------------------------------------------------------------
  47. void PrintManager::Add(EAMainChannel channel, IChannel* instance)
  48. {
  49. EA_ASSERT_MSG(instance, "invalid channel instance");
  50. EA_ASSERT_MSG(m_Channels[channel] == NULL, "channel already added to the list");
  51. if(instance != NULL && m_Channels[channel] == NULL)
  52. {
  53. // Initialize the channel, then add it to the channel vector.
  54. instance->Init();
  55. // Add the channel to the array
  56. m_Channels[channel] = instance;
  57. }
  58. }
  59. //------------------------------------------------------------
  60. void PrintManager::ClearChannel(EAMainChannel channel)
  61. {
  62. if (m_Channels[channel] != NULL)
  63. {
  64. IChannel* instance = m_Channels[channel];
  65. // Shut down the channel.
  66. instance->Shutdown();
  67. // Remove the channel from the array.
  68. m_Channels[channel] = NULL;
  69. }
  70. }
  71. //------------------------------------------------------------
  72. void PrintManager::Remove(EAMainChannel channel, IChannel* instance)
  73. {
  74. EA_ASSERT_MSG(instance, "invalid channel instance");
  75. EA_ASSERT_MSG(m_Channels[channel] != NULL, "channel not added to list yet");
  76. if(instance != NULL && m_Channels[channel] != NULL)
  77. {
  78. // Shut down the channel.
  79. instance->Shutdown();
  80. // Remove the channel from the array.
  81. m_Channels[channel] = NULL;
  82. }
  83. }
  84. //------------------------------------------------------------
  85. void PrintManager::Startup(const char8_t* printServerAddress)
  86. {
  87. // Register PrintManager print function with the reporting module.
  88. //
  89. #if EAMAIN_DISABLE_DEFAULT_NETWORK_CHANNEL
  90. if (!printServerAddress)
  91. {
  92. Add(CHANNEL_PRINTF, &gPrintfChannel);
  93. return;
  94. }
  95. #endif
  96. EA::EAMain::SetReportFunction(EA::EAMain::Messages::Print);
  97. if (!printServerAddress)
  98. {
  99. printServerAddress = MACRO_TO_STRING(EAMAIN_NETWORK_CHANNEL_IP);
  100. }
  101. const char8_t* server = printServerAddress;
  102. const char8_t* portStr = strchr(printServerAddress, ':');
  103. int port = EAMAIN_NETWORK_CHANNEL_PORT;
  104. char8_t serverBuff[64] = { 0 };
  105. if (portStr != NULL)
  106. {
  107. port = EA::StdC::AtoI32(portStr + 1);
  108. server = EA::StdC::Strncpy(serverBuff, server, portStr - server);
  109. }
  110. IChannel *networkChannel = Internal::CreateNetworkChannel(server, port);
  111. if (networkChannel)
  112. {
  113. Add(CHANNEL_NETWORK, networkChannel);
  114. }
  115. else
  116. {
  117. Add(CHANNEL_PRINTF, &gPrintfChannel);
  118. }
  119. }
  120. //------------------------------------------------------------
  121. void PrintManager::Shutdown()
  122. {
  123. for(int i = 0; i < CHANNEL_MAX; i++)
  124. {
  125. if(m_Channels[i])
  126. {
  127. m_Channels[i]->Shutdown();
  128. m_Channels[i] = NULL;
  129. }
  130. }
  131. }
  132. }}