how to get real time log via perforce api similar to p4v log

586 views Asked by At

I am facing issue with perforce api (.net), as i am unable to pull sync logs in real time.

- What am I trying to do

I am trying to pull real time logs as Sync is triggered using the
Perforce.P4.Client.SyncFiles() command. Similar to the P4V GUI Logs, which update when we try to sync any files.

- What is happening now

  • As the output is generated only after the command is done execution its not something intended for.

  • Also tried looking into Perforce.P4.P4Server.RunCommand() which does provide detailed report but only after the execution of the command. Looked into this

Reason is -

I am trying to add a status update to the Tool i am working on which shows which Perforce file is currently being sync'd.

Please advise. Thanks in Advance.

-Bharath

2

There are 2 answers

3
Samwise On BEST ANSWER

In the C++ client API (which is what P4V is built on), the client receives an OutputInfo callback (or OutputStat in tagged mode) for each file as it begins syncing.

Looking over the .NET documentation I think the equivalents are the P4CallBacks.InfoResultsDelegate and P4CallBacks.TaggedOutputDelegate which handle events like P4Server.InfoResultsReceived etc.

0
JulienH On

I ended up with the same issue, and I struggled quite a bit to get it to work, so I will share the solution I found:

First, you should use the P4Server class instead of the Perforce.P4.Connection. They are two classes doing more or less the same thing, but when I tried using the P4.Connection.TaggedOutputReceived events, I simply got nothing back. So instead I tried with the P4Server.TaggedOutputReceived, and there, finally, I got the TaggedOutput just like I wanted.

So, here is a small example:

P4Server p4Server = new P4Server(cwdPath); //In my case I use P4Config, so no need to set user or to login, but you can do all that with the p4Server here.
p4Server.TaggedOutputReceived += P4ServerTaggedOutputEvent;
p4Server.ErrorReceived += P4ServerErrorReceived;
bool syncSuccess=false;
try
{
    P4Command syncCommand = new P4Command(p4Server, "sync", true, syncPath + "\\...");
    P4CommandResult rslt = syncCommand.Run();
    syncSuccess=true;
    //Here you can read the content of the P4CommandResult
    //But it will only be accessible when the command is finished.
}
catch (P4Exception ex) //Will be caught only when the command has failed
{
    Console.WriteLine("P4Command failed: " + ex.Message);
}

And the method to handle the error messages or the taggedOutput:

private void P4ServerErrorReceived(uint cmdId, int severity, int errorNumber, string data)
{
    Console.WriteLine("P4ServerErrorReceived:" + data);
}

private void P4ServerTaggedOutputEvent(uint cmdId, int ObjId, TaggedObject Obj)
{
    Console.WriteLine("P4ServerTaggedOutputEvent:" + Obj["clientFile"]); //Write the synced file name.
    //Note that I used this only for a 'Sync' command, for other commands, I guess there might not be any Obj["clientFile"], so you should check for that.
}