- Since I have only 2300 gas available I can't call another contract that will make a log. Is it correct?
The basic cost for calling another contract is 700 gas (cost of CALL opcode). So it's probably possible to call another contract to do the logging, but that extra level of indirection might just be clutter. What's not possible is writing to storage, which is a basic cost of 5,000 gas.
A fallback function can get more than 2,300 gas, but unless you're calling your own fallback, you cannot assume that your fallback function will have more than 2,300 gas. For more information see How much computation can be done in a fallback function?
- What are my options then?
Since it's not possible to log before a throw, an option is to refund manually (and log it) if and only if ether was sent. If the manual refund fails, your only option to force a refund is to throw
but it's not possible to log this case.
contract C {
event error(uint8 error);
function() {
if (msg.value > 0) {
// manual refund and log it
if (msg.sender.send(msg.value)) {
error(1);
}
else {
// force a refund but cannot log this case
throw;
}
}
else {
error(2);
}
}
}
Note that msg.value > 0
is explicitly checked so that you can determine what you want done when msg.value
is zero. This example logs another error code, but you could also do nothing (by removing the second else
clause). When msg.value
is zero, I don't think you want error(1)
(as in @Xavier's answer) because no ether was sent by mistake to the contract and thus no refund was performed.