EAMainPrintManager.cpp 4.2 KB

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