How to get exit code in a pipe-lined command?

I have a question about how to get the exit code of the first command when it appears in a pipe-lined command.
For example, I have the following script:

grep abc dddd | tee -a log
if [[ $? -ne 0 ]]
then
echo "ERROR!"
fi

In the above script, [[ $? -ne 0 ]] is supposed to test the exit code of "grep abc dddd". But since it is in a pipe line, the $? actually stores the exit code of "tee -a log". So, [[ $? -ne 0 ]] is always false.

How to address this problem?

Thanks.

I did some research myself. I turned out that we can get the status code for each command in a pipeline from $PIPESTATUS array.

res=$(grep abc dddd)

if [[ $? -ne 0 ]]
then
      echo "ERROR!"
else
      echo $res >> log
fi

You can check the size of the logfile before and after you grep for the string. This way if the size of the logfile remains the same then you know the grep failed.