/**
 * Starts writing stats data to disk
 */
void FStatNotifyProvider_BinaryFile::StartWritingStatsFile()
{
	// NOTE: Make sure to update BinaryStatFileVersion if you change serialization behavior or data formats!

	if( File != NULL )
	{
		debugf( TEXT( "Stats System: Can't start capturing stats because a capture is already in progress." ) );
		return;
	}

	
	// Create the file
	if( !CreateOutputFile() )
	{
		debugf( TEXT( "Stats System: Couldn't create output file for stat capture." ) );
		return;
	}
	check( File != NULL );

	// We're expecting the files to be written out in big-endian since that's what StatsViewer wants
#if !( NO_BYTE_ORDER_SERIALIZE && !WANTS_XBOX_BYTE_SWAPPING )
	File->SetByteSwapping( TRUE );
#endif

	debugf( TEXT( "Stats System: Capturing stat data to disk." ) );

	FArchive& OutputFile = *File;


	// Write header
	{
		// NOTE: If you change the binary stats file format, update this version number as well
		//       as the version number in StatsViewerMisc.cpp!
		/* const */ DWORD BinaryStatFileVersion = 3;


		// Header tag (ASCII)
		const ANSICHAR* HeaderTagString = "USTATS";
		const INT HeaderTagLength = appStrlen( HeaderTagString );
		for( INT CharIndex = 0; CharIndex < HeaderTagLength; ++CharIndex )
		{
			BYTE CurCharByte = ( BYTE )HeaderTagString[ CharIndex ];
			OutputFile << CurCharByte;
		}

		// Version info
		OutputFile << BinaryStatFileVersion;

		// Number of seconds for each cycle value
		FLOAT SecondsPerCycleAsFloat = ( FLOAT )GSecondsPerCycle;
		OutputFile << SecondsPerCycleAsFloat;
	}

	
	// Force scoped cycle stats to be enabled.
	GForceEnableScopedCycleStats++;
}
/**
 * Tells the parent class to open the file and then writes the opening data to it
 */
UBOOL FStatNotifyProvider_CSV::Init(void)
{
	UBOOL bOk = FStatNotifyProvider_File::Init();
	if (bOk && ParseParam(appCmdLine(),TEXT("CSVStats")))
	{
		// CSV stat provider starts capturing immediately!
		bOk = CreateOutputFile();
	}
	return bOk;
}
/**
 * Tells the parent class to open the file and then writes the opening
 * XML data to it
 */
UBOOL FStatNotifyProvider_XML::Init(void)
{
	UBOOL bOk = FStatNotifyProvider_File::Init();
	if (bOk && ParseParam(appCmdLine(),TEXT("XMLStats")))
	{
		// XML stat provider starts capturing immediately!
		bOk = CreateOutputFile();
		if( bOk	)
		{
			// Create the opening element
			WriteString("<StatFile SecondsPerCycle=\"%e\">\r\n",GSecondsPerCycle);
		}
	}
	return bOk;
}
Esempio n. 4
0
bool ArticleDownloader::PrepareFile(char* szLine)
{
	bool bOpen = false;

	// prepare file for writing
	if (m_eFormat == Decoder::efYenc)
	{
		if (!strncmp(szLine, "=ybegin ", 8))
		{
			if (g_pOptions->GetDupeCheck())
			{
				m_pFileInfo->LockOutputFile();
				bool bOutputInitialized = m_pFileInfo->GetOutputInitialized();
				if (!bOutputInitialized)
				{
					char* pb = strstr(szLine, " name=");
					if (pb)
					{
						pb += 6; //=strlen(" name=")
						char* pe;
						for (pe = pb; *pe != '\0' && *pe != '\n' && *pe != '\r'; pe++) ;
						if (!m_szArticleFilename)
						{
							m_szArticleFilename = (char*)malloc(pe - pb + 1);
							strncpy(m_szArticleFilename, pb, pe - pb);
							m_szArticleFilename[pe - pb] = '\0';
						}
					}
				}
				if (!g_pOptions->GetDirectWrite())
				{
					m_pFileInfo->SetOutputInitialized(true);
				}
				m_pFileInfo->UnlockOutputFile();
				if (!bOutputInitialized && m_szArticleFilename && m_pFileInfo->IsDupe(m_szArticleFilename))
				{
					m_bDuplicate = true;
					return false;
				}
			}

			if (g_pOptions->GetDirectWrite())
			{
				char* pb = strstr(szLine, " size=");
				if (pb) 
				{
					m_pFileInfo->LockOutputFile();
					if (!m_pFileInfo->GetOutputInitialized())
					{
						pb += 6; //=strlen(" size=")
						long iArticleFilesize = atol(pb);
						if (!CreateOutputFile(iArticleFilesize))
						{
							m_pFileInfo->UnlockOutputFile();
							return false;
						}
						m_pFileInfo->SetOutputInitialized(true);
					}
					m_pFileInfo->UnlockOutputFile();
					bOpen = true;
				}
			}
			else
			{
				bOpen = true;
			}
		}
	}
	else
	{
		bOpen = true;
	}

	if (bOpen)
	{
		bool bDirectWrite = g_pOptions->GetDirectWrite() && m_eFormat == Decoder::efYenc;
		const char* szFilename = bDirectWrite ? m_szOutputFilename : m_szTempFilename;
		m_pOutFile = fopen(szFilename, bDirectWrite ? "rb+" : "wb");
		if (!m_pOutFile)
		{
			error("Could not %s file %s", bDirectWrite ? "open" : "create", szFilename);
			return false;
		}
		if (g_pOptions->GetWriteBufferSize() == -1)
		{
			setvbuf(m_pOutFile, (char *)NULL, _IOFBF, m_pArticleInfo->GetSize());
		}
		else if (g_pOptions->GetWriteBufferSize() > 0)
		{
			setvbuf(m_pOutFile, (char *)NULL, _IOFBF, g_pOptions->GetWriteBufferSize());
		}
	}

	return true;
}
Esempio n. 5
0
bool ArticleWriter::Start(Decoder::EFormat format, const char* filename, int64 fileSize,
	int64 articleOffset, int articleSize)
{
	m_outFile.Close();
	m_format = format;
	m_articleOffset = articleOffset;
	m_articleSize = articleSize ? articleSize : m_articleInfo->GetSize();
	m_articlePtr = 0;

	// prepare file for writing
	if (m_format == Decoder::efYenc)
	{
		if (g_Options->GetDupeCheck() &&
			m_fileInfo->GetNzbInfo()->GetDupeMode() != dmForce &&
			!m_fileInfo->GetNzbInfo()->GetManyDupeFiles())
		{
			bool outputInitialized;
			{
				Guard guard = m_fileInfo->GuardOutputFile();
				outputInitialized = m_fileInfo->GetOutputInitialized();
				if (!g_Options->GetDirectWrite())
				{
					m_fileInfo->SetOutputInitialized(true);
				}
			}

			if (!outputInitialized && filename &&
				FileSystem::FileExists(BString<1024>("%s%c%s", m_fileInfo->GetNzbInfo()->GetDestDir(), PATH_SEPARATOR, filename)))
			{
				m_duplicate = true;
				return false;
			}
		}

		if (g_Options->GetDirectWrite())
		{
			Guard guard = m_fileInfo->GuardOutputFile();
			if (!m_fileInfo->GetOutputInitialized())
			{
				if (!CreateOutputFile(fileSize))
				{
					return false;
				}
				m_fileInfo->SetOutputInitialized(true);
			}
		}
	}

	// allocate cache buffer
	if (g_Options->GetArticleCache() > 0 && !g_Options->GetRawArticle() &&
		(!g_Options->GetDirectWrite() || m_format == Decoder::efYenc))
	{
		m_articleData = g_ArticleCache->Alloc(m_articleSize);

		while (!m_articleData.GetData() && g_ArticleCache->GetFlushing())
		{
			Util::Sleep(5);
			m_articleData = g_ArticleCache->Alloc(m_articleSize);
		}

		if (!m_articleData.GetData())
		{
			detail("Article cache is full, using disk for %s", *m_infoName);
		}
	}

	if (!m_articleData.GetData())
	{
		bool directWrite = (g_Options->GetDirectWrite() || m_fileInfo->GetForceDirectWrite()) && m_format == Decoder::efYenc;
		const char* outFilename = directWrite ? m_outputFilename : m_tempFilename;
		if (!m_outFile.Open(outFilename, directWrite ? DiskFile::omReadWrite : DiskFile::omWrite))
		{
			m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError,
				"Could not %s file %s: %s", directWrite ? "open" : "create", outFilename,
				*FileSystem::GetLastErrorMessage());
			return false;
		}
		SetWriteBuffer(m_outFile, m_articleInfo->GetSize());

		if (directWrite)
		{
			m_outFile.Seek(m_articleOffset);
		}
	}

	return true;
}